Dail
Dail

Reputation: 4606

How can I 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 those routes all in one?

Upvotes: 4

Views: 1274

Answers (3)

Larry Kooper
Larry Kooper

Reputation: 71

I modified Jone Polvera's answer slightly to use a 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 within routes I have a file called admin.js:

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

So the URL /admin/posts is routed there.

Upvotes: 0

Jone Polvora
Jone Polvora

Reputation: 2338

Currently I'm trying to accomplish to get routes like ASP.NET MVC routes, by 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 controllers inside that 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');
};

Upvotes: 1

Raynos
Raynos

Reputation: 169383

No you can't. That's not how you should do routing. Routes should be well define to have sensible uris.

For example I've hand written 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 you have complete control over the URIs you want.

It's highly adviced you define the uris you want manually and hook them upto to whatever controller object you want.

This means that your uris stay pretty, semantic, and in full control.

Upvotes: 6

Related Questions