60

I start to look at Node.js. Also I'm using Express. And I have a question - how can I organize web application routes? All examples just put all this app.get/post/put() handlers in app.js and it works just fine. This is good but if I have something more than a simple HW Blog? Is it possible to do something like this:

var app = express.createServer();
app.get( '/module-a/*', require('./module-a').urls );
app.get( '/module-b/*', require('./module-b').urls );

and

// file: module-a.js
urls.get('/:id', function(req, res){...}); // <- assuming this is handler for /module-a/1

In other words - I'd like something like Django's URLConf but in Node.js.

verybadalloc
  • 5,768
  • 2
  • 33
  • 49
NilColor
  • 3,462
  • 3
  • 30
  • 44

6 Answers6

96

I found a short example in ´Smashing Node.js: JavaScript Everywhere´ that I really liked.

By defining module-a and module-b as its own express applications, you can mount them into the main application as you like by using connects app.use( ) :

module-a.js

module.exports = function(){
  var express = require('express');
  var app = express();

  app.get('/:id', function(req, res){...});

  return app;
}();

module-b.js

module.exports = function(){
  var express = require('express');
  var app = express();

  app.get('/:id', function(req, res){...});

  return app;
}();

app.js

var express = require('express'),
    app = express();

app.configure(..);

app.get('/', ....)
app.use('/module-a', require('./module-a'));    
app.use('/where/ever', require('./module-b'));    

app.listen(3000);

This would give you the routes

localhost:3000/
localhost:3000/module-a/:id
localhost:3000/where/ever/:id
Vegar
  • 12,828
  • 16
  • 85
  • 151
  • 6
    As I said: This is taken from Guillermo Rauchs book, so I'll just blame him for all potential mind damage :-) – Vegar Jun 03 '13 at 08:53
  • By this approach, `app`, `module-a`, `module-b` all get it's own *express app* instance, is this good? – Andrew_1510 Oct 22 '13 at 06:04
  • 1
    The book does not mention any down sides. I would guess that having multiple application instances will take more resources then not having multiple instances, but how much? And I suppose it would be possible to overuse this method too. I wouldn't make every little route its own application, but if the application consist of multiple separated concerns, like a blog and a web store, I find it quite neat. – Vegar Oct 22 '13 at 11:14
  • 17
    You can DRY this up by passing in your express app as a param. Then, you don't need to make multiple express applications. Setup like this: `module.exports = function(app) {...}` and then just call `app.use('/module-a', require('./module-a')(app)); – mattmc3 Oct 27 '13 at 02:24
  • 2
    @mattmc3, your solution gives me the following error: Error: Cyclic \_\_proto\_\_ value – Michael Feb 01 '14 at 10:26
  • 1
    @mattmc3 I think it should be a bit more like this: `module-b.exports = function(app){ return function(){ app.get('/:id',function(req, res){...}); return app; }; }; app.use('/where/ever', require('./module-b')(app));` – Timo Huovinen May 19 '14 at 14:47
  • 1
    In the express 4 somethings have changed. I have found this article what solve my migration problems http://scotch.io/tutorials/javascript/learn-to-use-the-new-router-in-expressjs-4. – Thiago Mata Nov 14 '14 at 19:24
  • I had problems with that link. But i think this one works: http://scotch.io/tutorials/javascript/learn-to-use-the-new-router-in-expressjs-4 – Vegar Nov 22 '14 at 13:31
  • Notice that: (1) `app.use('/prefix', router)` works fine; (2) node files run into a closure. Thus, quite simply cut your routes from the main file and paste them into their own `./app/my-component/routes.js`, top it with `router = require('express').Router();`, and bottom it with `module.exports = router`. Then, in main add: `app.use('/prefix', require('./app/my-component/routes'))`. – aercolino Oct 22 '15 at 17:39
  • setup router module: module.exports = function(express){ var router = express.Router(); .... return router }; setup app: app.use('/module-a/', require('./module-a/')(express)); – Aderbal Nunes Oct 26 '15 at 13:29
29

Check out the examples here:

https://github.com/visionmedia/express/tree/master/examples

'mvc' and 'route-separation' may be helpful.

TK-421
  • 10,598
  • 3
  • 38
  • 34
8

There also is a screencast of @tjholowaychuk (creator of express) where he uses the method @Vegar described.

Available on Vimeo: Modular web applications with Node.js and Express

Community
  • 1
  • 1
Koen.
  • 25,449
  • 7
  • 83
  • 78
3

One more alternative;

App.js

var express = require('express')
      , routes = require('./routes')
      , user = require('./routes/user')
      , http = require('http')
      , path = require('path');

    var app = express();


// all environments
app.set('port', process.env.PORT || 3000);


app.get('/', routes.index);
app.get('/users/:id', user.getUser);

http.createServer(app).listen(app.get('port'), function(){
  console.log('Express server listening on port ' + app.get('port'));
});

index.js

exports.index = function(req, res){
  res.render('index', { title: 'Express' });
};

user.js

exports.getUser = function(req, res){


    //your code to get user

};
hadaytullah
  • 1,164
  • 13
  • 14
2

Check out the article about the express-routescan node module. This module helps to organize maintainable routing for express application. You can try it. This is the best solution for me.

1

There are several ways to do:

1:

module1(app.route('/route1'));
module2(app.route('/route2'));

In the modules you can just implement 1 function to handle the http methods:

module.exports = function(route) {
   route
   .get(function(req, res, next) {
       ...
   }).
   .post(function(req, res, next) {
      ...
   });
}

2: if you want to handle the route by a sub-app instead of a module/middleware :

var m1 = require(module1.js);
var m2 = require(module2.js);

app.use('/route1', r1);
app.use('/route2', r2);
Minh Hoang
  • 21
  • 1