
Security News
Meet Socket at Black Hat and DEF CON 2025 in Las Vegas
Meet Socket at Black Hat & DEF CON 2025 for 1:1s, insider security talks at Allegiant Stadium, and a private dinner with top minds in software supply chain security.
expressjs-router
Advanced tools
Structured routing for Express.JS
Every route is implemented in a dedicated file
Example:
.
+-- routes
| +-+ todos
| +-- create.js
| +-- update.js
| +-- delete.js
+-- models
| +-- todo.js
+-- server.js
routes/todos/create.js
// File: ./routes/todos/create.js
'use strict';
const Todo = require("../../models/todo");
// The validate function is executed before the responder, allowing
// to easily check for the existence of parameters and their format.
// If some parameters are not valid, the function must throw an error
//
// Note: Can be either synchronous or asynchronous.
function validate(req) {
if (!req.body.text) {
throw new Error('Missing parameter "text"')
}
}
// The respond method is the actual implementation of our route
async function respond(req, res, next) {
let { text } = req.body;
let user = req.preloadedData.user;
let todo = new Todo({
text,
user,
});
await todo.validate();
await todo.save();
res.status(201);
res.json({ todo });
}
// The exports describe the route definition.
// .path, .method and .respond are required
//
// Conditions are checked to define wether the route should be
// executed or not. You can define as many conditions as you want.
export default {
path: '/todos',
method: 'POST',
validate
respond,
conditions : [ 'isLoggedIn' ],
preload : [ 'user' ],
};
server.js
// File: ./app.js
'use strict';
import express from 'express';
import router from 'expressjs-router';
const app = express();
// By enabling debug mode, routes will be logged to stdout
router();
// Conditionals allow us to add handy condition checks all over our app
router.createConditional('isLoggedIn', async (req) => {
return !!req.session.user;
});
// Preloaders allow us to easily preload resources before the requests are executed
router.createPreloader('user', async (req) => {
return await User.findById(req.session.user._id);
});
// You can decide if either you want to preload resources sequentially or in parallel
//
// 0 -> parallel (default)
// 1 -> sequential
//
router.setPreloadingMode(1);
// Finally, create routes by padding the path where they're stored
router.create(app, "./path/to/routes");
app.listen(80);
_
or named index.js
won't be includedrouter.enableDebug();
express-validator
ALL
to match any HTTP methodmethod
to be an array of HTTP methodsMIT
FAQs
Structured routes for ExpressJS
We found that expressjs-router demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
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.
Security News
Meet Socket at Black Hat & DEF CON 2025 for 1:1s, insider security talks at Allegiant Stadium, and a private dinner with top minds in software supply chain security.
Security News
CAI is a new open source AI framework that automates penetration testing tasks like scanning and exploitation up to 3,600× faster than humans.
Security News
Deno 2.4 brings back bundling, improves dependency updates and telemetry, and makes the runtime more practical for real-world JavaScript projects.