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

express-file-router-2

Package Overview
Dependencies
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

express-file-router-2

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

  • 1.0.2
  • latest
  • npm
  • Socket score

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

express-file-router

Installation

npm install express-file-router

How to Use

You can integrate the file router by using it as a middleware like this:

app.use(
  "/api",
  await FileRouter(
    {
      ROUTES_DIR: "/routes", // directory of your routes
      debug: true, // simple console.log's
    },
    __dirname
  )
);

Example Express Setup

import express from "express";
import cors from "cors";
import bodyParser from "body-parser";
import { createServer } from "http";
import FileRouter from "express-file-router-2";

async function init(): Promise<void> {
  process.on("uncaughtException", (err) => {
    console.log(err);
  });

  const app = express();

  app.use(cors());
  app.use(
    bodyParser.json({
      limit: "50mb",
    })
  );
  app.use(bodyParser.urlencoded({ extended: true, limit: "50mb" }));

  app.use(
    "/api",
    await FileRouter(
      {
        ROUTES_DIR: "/routes",
        debug: true,
      },
      __dirname
    )
  );

  const server = createServer(app);
  const port = 3080;

  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
    └── post.ts // post

Middleware

You are able to add route specific middlewares by exporting an array like this:

Post Example

import { RequestHandler } from "express";
import UserSession from "@/middleware/session/user";

export const post = [
  // inside of file
  async (req, res, next) => {
    console.log("headers", req.headers);

    return next();
  },
  // imported middelware from file
  UserSession,
  async (req, res) => {
    const { userID } = req.params;

    console.log("req.params", req.params);

    return res.status(200).json({
      message: "Success",
      userID,
    });
  },
] as RequestHandler[];

Get Example

import { RequestHandler } from "express";
import UserSession from "@/middleware/session/user";

export const get = [
  // inside of file
  async (req, res, next) => {
    console.log("headers", req.headers);

    return next();
  },
  // imported middelware from file
  UserSession,
  async (req, res) => {
    const { userID } = req.params;

    console.log("req.params", req.params);

    return res.status(200).json({
      message: "Success",
      userID,
    });
  },
] 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