Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

socketio-file-router

Package Overview
Dependencies
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

socketio-file-router

An express router that allows you to use the file system to create routes.

  • 1.0.0
  • latest
  • npm
  • Socket score

Version published
Weekly downloads
0
decreased by-100%
Maintainers
1
Weekly downloads
 
Created
Source

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

Keywords

FAQs

Package last updated on 07 Nov 2023

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc