File routing system-based framework like api serverless for Node Express with 0
dependencies.
npm install express-file-based
- app.(js|ts) (main)
const express = require("express")
const { fileBased } = require("express-file-based")
const app = express()
app.use('/', fileBased()) // uses /routes directory by default
app.listen(2000)
- routes/index.js
module.exports = async (req, res) => {
if (req.method !== "GET") return res.status(404)
return res.status(200)
}
Files inside your project's /routes
directory will get matched an url path automatically.
├── app.js
├── routes
├── index.js
├── posts
├── index.js
└── :id.js or [id].js // dynamic req.params.id
└── users.js
└── package.json
/routes
→ //routes/posts
→ /posts/routes/posts/:id
→ /posts/:id/routes/users
→ /users
app.use('/api', fileBased({
directory: 'routes',
methodExports: ["ws", ...]
}))
.use('/anotherrouter', fileBased({
directory: 'anoherfile',
}));
directory
: The path to the routes directory (default /routes)methodExports
: Additional method exports (e.g.ws
for express-ws)verbose
: show routes on console (default process.env.NODE_ENV !== 'production' )options
: RouterOptions { caseSensitive/mergeParams/strict }
If you export functions named e.g. get
, post
, put
, delete
/del
etc. those will get matched their corresponding http method automatically.
module.exports.priority = 1; // kind of like a z-index for your routes.
/**
* @param {Express.Request} req
* @param {Express.Response} res
*/module.exports.get = async (req, res) => { ... }
/**
* @param {Express.Request} req
* @param {Express.Response} res
*/module.exports.post = async (req, res) => { ... }
/**
* @param {Express.Request} req
* @param {Express.Response} res
*/module.exports.put = async (req, res) => { ... }
/**
* @param {Express.Request} req
* @param {Express.Response} res
* it's not allowed to name variables 'delete': try 'del' instead
*/module.exports.del = async (req, res) => { ... }
/**
* @param {Express.Request} req
* @param {Express.Response} res
* use default for _all
*/module.exports.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.
const { withAuth } = require("../middlewares")
module.exports.get = [
withAuth,
async (req, res) => { ... }
]