
Security News
ECMAScript 2025 Finalized with Iterator Helpers, Set Methods, RegExp.escape, and More
ECMAScript 2025 introduces Iterator Helpers, Set methods, JSON modules, and more in its latest spec update approved by Ecma in June 2025.
express-iroute
Advanced tools
An express route integrated with an simple interceptor system.
$ npm install express-iroute
$ yarn add express-iroute
As this module is primarily designed for routes integrated with interceptors, the first thing is to define interceptors.
// interceptors/index.js
module.exports = [
{
flag: 'REQUIRE_MOBILE',
path: '/*',
preHandler(req, res, next) {
if (req.headers['user-agent'].indexOf('mobile') === -1) {
res.redirect('http://www.foo.com');
return;
}
next();
},
},
{
flag: 'REQUIRE_LOGIN',
path: /^\/(?!login)/,
preHandler(req, res, next) {
const userInfo = userService.getUserInfo(req);
if (!userInfo) {
res.redirect(`/login?redirecturl=${encodeURIComponent(req.url)}`);
return;
}
next();
},
},
{
flag: 'REQUIRE_CSRF',
preHandler(req, res, next) {
// do some csrf check
},
},
{
flag: 'EXACT_REQUIRE_CORS',
exact: true,
preHandler(req, res, next) {
// do some cors check
},
},
];
exact: Boolean
can be used as the trigger to turn on or off the perfect path matching pattern, the path matching rules can be found in path-to-regexp@v0.1.7 (the same version used in express).
flag: String
is the identity of interceptor, used for specific route to add or ignore interceptors.
path: String|RegExp|Array
defines paths which this interceptor should apply, following express route path format. If not defined, no routes will apply except some specific route configs.
preHandler: Function
defines interceptor function, which executes before actual route handler.
This module heavily borrows idea and code from express-autoroute, so routes definition is exactly the same with it. Only one thing is different: you can config interceptors
or ignoreInterceptors
to overwrite interceptor-level configuration.
// routes/login/login.js
module.exports = [
{
path: '/',
interceptors: 'APPEND_CSRF',
handler: loginGetController,
},
{
path: '/',
method: 'POST',
interceptors: 'REQUIRE_CSRF',
handler: loginPostController,
},
{
path: '/randomcode',
ignoreInterceptors: 'REQUIRE_LOGIN',
handler: randomcodePngController,
},
];
Every route module can export an route object or a list of route object.
// single route object
module.exports = {
path: '/category',
method: 'GET',
handler(req, res, next) {
// do something...
},
};
// list of route objects
module.exports = [
{
path: '/category/:id',
method: 'GET',
handler(req, res, next) {
// do something...
},
},
{
path: '/category/:id',
method: 'POST',
interceptors: 'REQUIRE_LOGIN',
handler(req, res, next) {
// do something...
},
},
];
path: String
route path, will be prefixed with directory path.
method: String|Array
http verb, default is 'GET'.
interceptors: String|Array
overwrite common config, add an interceptor or some interceptors.
ignoreInterceptors: String|Array
overwrite common config, remove an interceptor or some interceptors.
handler: Function|Array
route handlers.
Route API is just a simple wrapper of express route methods, please refer to its document for details.
const express = require('express');
const iroute = require('express-iroute');
const interceptors = require('./interceptors');
const app = express();
iroute(app, {
interceptors,
routesDir: './routes',
});
Thanks for good ideas from express-autoroute and HandlerInterceptor from SpringMVC.
FAQs
express route integrated with interceptor system
The npm package express-iroute receives a total of 14 weekly downloads. As such, express-iroute popularity was classified as not popular.
We found that express-iroute demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 2 open source maintainers 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
ECMAScript 2025 introduces Iterator Helpers, Set methods, JSON modules, and more in its latest spec update approved by Ecma in June 2025.
Security News
A new Node.js homepage button linking to paid support for EOL versions has sparked a heated discussion among contributors and the wider community.
Research
North Korean threat actors linked to the Contagious Interview campaign return with 35 new malicious npm packages using a stealthy multi-stage malware loader.