socketio-file-router
TypeScript icon, indicating that this package has built-in type declarations

1.0.0 • Public • Published

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
);

Example Setup

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

Structure Setup

├── 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[];

Package Sidebar

Install

npm i socketio-file-router

Weekly Downloads

0

Version

1.0.0

License

ISC

Unpacked Size

37.6 kB

Total Files

13

Last publish

Collaborators

  • isaacjuracich