Simple file routing for express. Unlike other packages, Express Super Router uses express's own Router()
function instead of a custom made one.
// ES6 modules
import superrouter from 'express-super-router';
import express from 'express';
// CommonJS
const superrouter = require('express-super-router');
const express = require('exress');
const app = express();
app.use(express.json());
// Adding routes
app.use('/api', superrouter('./src/routes'));
// all Router instances within 'src/routes' will now be added
app.listen(8080);
- yarn:
yarn add express-super-router
- npm:
npm i express-super-router
The main function for Express Super Router, this will pretty much be the only function you need.
superrouter(dir: string, options?: SuperRouteOptions): Express.Router
Example:
const express = require('express');
const superrouter = require('express-super-router');
express.use('/api', superrouter('src/routes'));
Simple interface for the options of the Express Super Router function
interface SuperRouterOptions {
recursive?: boolean,
ignorePrefix?: string
}
Routes must have an export of Express.Router()
example:
/* src/routes/index.ts */
const { Router } = require('express');
const route = Router();
route.get('/', (_, res) => {
res.send({ '' })
})
module.export = route;
Express Super Router only changes the express name refferences slightly for params, instead of using :id
you should use [id]
examples:
/index.js
=>/
/index/about.js
=>/index/about
/users/[id].js
=>/users/:id
/files/[user][repo]
=>/files/:user/:repo