
Security News
Deno 2.2 Improves Dependency Management and Expands Node.js Compatibility
Deno 2.2 enhances Node.js compatibility, improves dependency management, adds OpenTelemetry support, and expands linting and task automation for developers.
express-route-controller2
Advanced tools
This is a helper function to assign controller actions to routes in express
This is a helper function to assign controller actions to routes in express based on express-route-controller.
It provides just a structure on top of a normal express app, but also doesn't get in the way at all, and you can continue to use express normally and define even more routes manually if you wish. It's focused to help in the creation of APIs.
In your express project install express-route-controller2:
npm install express-route-controller2
Now create a folder where you want all your controllers, eg. controllers
, and add a file in there,
named mycontroller.js
. Then define it somehow, like so:
module.exports = {
create: function(req, res) {
},
update: function(req, res) {
},
read: function(req, res) {
},
delete: function(req, res) {
},
validate: function(req, res, next) {
}
};
In your main app.js file (or wherever you set up express routes normally) simply call the helper function (very sparse demo express app):
var express = require('express');
var app = express();
var erc2 = require('express-route-controller2');
// set up express route control:
erc2(app, {
controllers: __dirname + '/controllers',
routes: {
"/grades": "mycontroller#read", // method: get
"/notices/:id": {
"get": "mycontroller#read",
"delete": "mycontroller#delete",
"put": {
"middleware": "mycontroller#validate", // this could be an array eg: ["mc#1", "mc#2"]
"handler": "mycontroller#update"
}
}
}
});
app.listen(3000);
You can make this even more easier, by defining your routes in a routes.json
file, like so:
{
"/grades": "mycontroller#read",
"/notices/:id": {
"get": "mycontroller#read",
"delete": "mycontroller#delete",
"put": {
"middleware": "mycontroller#validate",
"handler": "mycontroller#update"
}
}
}
And loading the routes is as simple as:
...
erc2(app, {
controllers: __dirname + '/controllers',
routes: require('routes.json')
});
...
Pull requests, feature ideas and bug reports are welcome
MIT
FAQs
This is a helper function to assign controller actions to routes in express
We found that express-route-controller2 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
Deno 2.2 enhances Node.js compatibility, improves dependency management, adds OpenTelemetry support, and expands linting and task automation for developers.
Security News
React's CRA deprecation announcement sparked community criticism over framework recommendations, leading to quick updates acknowledging build tools like Vite as valid alternatives.
Security News
Ransomware payment rates hit an all-time low in 2024 as law enforcement crackdowns, stronger defenses, and shifting policies make attacks riskier and less profitable.