Simple system-based file routing for Express


341.2.03 months ago5 months agoMinified + gzip package size for express-file-routing in KB



Flexible system-based file routing for Express with 0 dependencies.


npm install express-file-routing

Note: If you prefer yarn instead of npm, just use yarn add express-file-routing.

How to use

  • app.ts (main)
import express from "express"
import createRouter, { router } from "express-file-routing"

const app = express()

app.use("/", router()) // as router middleware or

createRouter(app) // as wrapper function


Note: It uses your project's /routes directory as source by default.

  • routes/index.ts
export default async (req, res) => {
  if (req.method !== "GET") return res.status(404)

  return res.status(200)

Directory Structure

Files inside your project's /routes directory will get matched an url path automatically.

├── app.ts
├── routes
    ├── index.ts // index routes
    ├── posts
        ├── index.ts
        └── :id.ts or [id].ts // dynamic params
    └── users.ts
└── package.json
  • /routes/index.ts → /
  • /routes/posts/index.ts → /posts
  • /routes/posts/:id.ts → /posts/:id
  • /routes/users.ts → /users


createRouter(app, {
  directory: path.join(__dirname, "routes"),
  methodExports: ["ws", ...]
// or
  directory: path.join(__dirname, "routes"),
  methodExports: ["ws", ...]


  • directory: The path to the routes directory (default /routes)
  • methodExports: Additional method exports (e.g. ws for express-ws)


HTTP Method Matching

If you export functions named e.g. get, post, put, delete/del etc. those will get matched their corresponding http method automatically.

export const get = async (req, res) => { ... }

export const post = async (req, res) => { ... }

// it's not allowed to name variables 'delete': try 'del' instead
export const del = async (req, res) => { ... }

// you can still use a wildcard default export in addition
export default async (req, res) => { ... }

Note: Named method exports gain priority over wildcard exports (= default exports).


You can add isolated, route specific middlewares by exporting an array of Express request handlers from your route file.

import { rateLimit, bearerToken, userAuth } from "../middlewares"

export const get = [
  rateLimit(), bearerToken(), userAuth(),
  async (req, res) => { ... }

Custom Methods Exports

You can add support for other method exports to your route files.

// app.ts
import ws from "express-ws"

const { app } = ws(express())

createRouter(app, {
  methodExports: ["ws"]

// routes/index.ts
export const ws = async (ws, req) => {

If you find any bugs or have a feature request, please open an issue on github!

The npm package download data comes from npm's download counts api and package details come from