socketio-file-router
v1.0.0
Published
An express router that allows you to use the file system to create routes.
Downloads
3
Maintainers
Readme
socketio-file-router
Installation
npm install socketio-file-router
How to Use
You can integrate the file router by using it as a middleware like this:
await FileRouter(
io, // socket server
{
ROUTES_DIR: "/routes",
debug: false,
},
__dirname
);
import { createServer } from "http";
import { Server } from "socket.io";
import FileRouter from "socketio-file-router";
async function init(): Promise<void> {
process.on("uncaughtException", (err) => {
console.log(err);
});
const port = 3080;
const server = createServer();
const io = new Server(server, {
cors: {
origin: "*",
},
});
await FileRouter(
io,
{
ROUTES_DIR: "/routes",
debug: false,
},
__dirname
);
server.listen(port, () => console.log(`Server listening on port ${port}`));
}
init();
Route Setup
Example Structure
├── index.ts // main file
├── routes
├── get.ts // get
├── dynamic // params
├── param
├── [example].ts // single
└── [...slug].ts // get all
└── user
├── index.ts // user
└── post.ts
Middleware
You are able to add route specific middlewares by exporting an array like this: Example
import { RequestHandler } from "socketio-file-router";
export const handler = [
async (socket, params, args, next) => {
if (!next) return;
return next({
nextParams: {
userID: "123",
},
});
},
async (socket, params, args) => {
console.log("middleware post", params, args);
},
] as RequestHandler[];