This module is a simple recursive filesystem reader that allows you to define routes in a filesystem structure.
$ npm install express-router-filesystem
or, using yarn
$ yarn add express-router-filesystem
const express = require('express');
const router = require('express-router-filesystem');
const app = express();
app.use(router({
routesDir: __dirname + '/routes',
routePrefix: '/api'
}));
app.listen(3000);
or using imports
import express from 'express';
import router from 'express-router-filesystem';
const app = express();
app.use(router({
routesDir: __dirname + '/routes',
routePrefix: '/api'
}));
app.listen(3000);
Export a function that takes the express router as an argument. The function name must be the method to match. The function should return the router.
// routes/users/get.js
module.exports = {
get(req, res) {
res.send('GET /users');
};
};
or using exports
// routes/users/get.js
export const get = (req, res) => {
res.send('GET /users');
};
Use brackets to assign a named parameter to a route. The value of the parameter will be available in the request object as req.params.<name>
.
// routes/users/[id].js
module.exports = {
get(req, res) {
res.send('GET /users, id ' + req.params.id);
};
};
or using exports
// routes/users/[id].js
export const get = (req, res) => {
res.send('GET /users, id ' + req.params.id);
};
You can define middlewares for a route by creating a file with the name _middleware.js
. The file should export a default function that takes the express router as an argument. The function should return the router.
// routes/users/_middleware.js
module.exports = (req, res, next) => {
console.log('middleware');
next();
};
or using exports
// routes/users/_middleware.js
export default (req, res, next) => {
console.log('middleware');
next();
};
Any middleware defined in the _middleware.js
file will be applied to all routes in the directory and its subdirectories. For example, if you have a route structure like this:
- routes
- _middleware.js (1)
- users
- _middleware.js (2)
- [id].js
- index.js
- posts
- _middleware.js (3)
- [id].js
- index.js
Calling /users/1 will call the middlewares numbers 1 and 2. Calling /users/posts/1 will call the middlewares numbers 1, 2 and 3.
You can also define multiple middlewares for a route by naming the file as _middleware.<name>.js
. The name can be alphanumeric characters, -
and _
. Middlewares will be called in alphabetical order.
Examples:
_middleware.1-login.js
_middleware.session.js
_middleware.2_authorization_token.js
The absolute path to the directory containing the routes. This is required.
A prefix to add to all routes. This is optional.