How to optimize routes in Express.JS?

I have many routes like:

app.all('/:controller', controller.init()); app.all('/:controller/:action', controller.init()); app.all('/:controller/:action/:par1', controller.init()); app.all('/:controller/:action/:par1/:par2', controller.init()); app.all('/:controller/:action/:par1/:par2/:par3', controller.init()); 

Can I optimize these routes in one?

+4
source share
3 answers

No, you can’t. This is not how you should do routing. Routes must be well defined in order to have reasonable uris.

For example, I wrote the following routes

app.get ("/ blog", controller.index);

 app.get("/blog/new", controller.renderCreate); app.get("/blog/:postId/edit", controller.renderEdit); app.get("/blog/:postId/:title?", controller.view); app.post("/blog", controller.createPost); app.put("/blog/:postId", controller.updatePost); app.del("/blog/:postId", controller.deletePost); 

This means that you have full control over the URIs you want.

You are strongly advised to define the uris you want manually and connect them to any controller object that you want.

This means that your uris remains pretty, semantic and fully controlled.

+6
source

I'm currently trying to execute routes, such as ASP.NET MVC routes, doing something like:

 app.all('*', function(req, res) { //extract the route into an array var m_path = req.route.params.toString().split('/'); //require for a module with a dynamic name based on path info var controller = require('./controllers/' + m_path[1] + '.js'); //build a method name var fname = req.route.method + (m_path[2] || 'Index'); //if a exported method exists on the module, then use it, otherwise, create a new function var func = controller[fname] || function (){ //maybe use a 404 res.send('controller/method not found: ' + fname); }; //invoke the function func.call(this, req, res); }); 

In this example, I have a folder called controllers. Then I put all the controllers in this folder. Then I can do something like this:

route: / users /

js: controllers / users.js

 //template: GET /users/ module.exports.getIndex = function(req, res) { res.send('get on index'); }; //template: POST /users/index module.exports.postIndex = function(req, res) { res.send('post on index'); }; //template: PUT /users/index module.exports.putIndex = function(req, res) { res.send('put on index'); }; //template: GET /users/custom module.exports.getCustom = function(req, res) { res.send('get on custom'); }; 
+1
source

I slightly modified Jone Polvera to use regex:

 app.all(/^(.+)\/(.+)/, function(req, res) { var controller = require('./routes/' + req.params[0] + '.js'); var fname = req.params[1]; var func = controller[fname] || function () { res.send('controller/method not found: ' + fname); }; func.call(this, req, res); }); 

I have a folder called routes. Then in the routes I have the admin.js file:

 exports.posts = function(req, res) { ... } 

Thus, the URL / admin / posts is routed there.

0
source

Source: https://habr.com/ru/post/1384470/


All Articles