HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavascriptModerate

Exporting routes in node.js Express 4

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
exportingnoderoutesexpress

Problem

I'm using Express 4 to provide routes to HTML/Jade files, and as well to provide an API.

I want to separate routes from server file, and go even further- separate API from WWW. My code now looks like this:

server.js

//================== ROUTES ======================================

require('./routes/api')(app, express);  //all api routes go in ./routes/api.js file
require('./routes/web')(app, express);  //all website routes go in ./routes/web.js file


apiroutes:

//================================== routes for API ====================================
module.exports = function(app, express) {
    'use strict';
    var api = express.Router();

    api.get('/somget', function(req, res) {
        res.send('some json');
    });

    // init api route. all api routes will begin with /api
    app.use('/api', api);

}


pages routes (both main template and partials):

//================================== routes for index and partials================================

module.exports = function(app, express) {
    'use strict';
    var router = express.Router();

    router.get('/', function(req, res) {
        res.render('index');
    });

    router.get('/partials/:name', function (req, res) {
        var name = req.params.name;
        res.render('partials/' + name);
    });

    //apply router
    app.use('/', router);

};


Is it okay to pass both app and express?

It works, but I'm not sure if such solution may have some bad impact on app and routing -passing both app and express to two functions.

Solution

You don't have to inject express into each modules.

requiring express in each file will return the same instance. http://nodejs.org/docs/latest/api/modules.html#modules_caching

Also, ask yourself who should be the one registering the router into the app. The app it self or the router ? For code reuse and clean separation you should let the app handle it. Let's say you have an authentication router used with the path '/auth'. You may want to reuse it in another app with a path like '/authentication'

Here's how I would do it.

server.js

//================== ROUTES ======================================
...

var api = require('./routes/api');
var router = require('./routes/web');

app.use('/api', api);
app.use('/', router);

...


apiroutes:

//================================== routes for API ====================================

var express = require('express');

module.exports = (function() {
    'use strict';
    var api = express.Router();

    api.get('/somget', function(req, res) {
        res.send('some json');
    });

    return api;
})();


pages routes (both main template and partials):

//================================== routes for index and partials================================

var express = require('express');

module.exports = (function() {
    'use strict';
    var router = express.Router();

    router.get('/', function(req, res) {
        res.render('index');
    });

    router.get('/partials/:name', function (req, res) {
        var name = req.params.name;
        res.render('partials/' + name);
    });

    return router;    
})();

Code Snippets

//================== ROUTES ======================================
...

var api = require('./routes/api');
var router = require('./routes/web');

app.use('/api', api);
app.use('/', router);

...
//================================== routes for API ====================================

var express = require('express');

module.exports = (function() {
    'use strict';
    var api = express.Router();

    api.get('/somget', function(req, res) {
        res.send('some json');
    });

    return api;
})();
//================================== routes for index and partials================================

var express = require('express');

module.exports = (function() {
    'use strict';
    var router = express.Router();

    router.get('/', function(req, res) {
        res.render('index');
    });

    router.get('/partials/:name', function (req, res) {
        var name = req.params.name;
        res.render('partials/' + name);
    });

    return router;    
})();

Context

StackExchange Code Review Q#51614, answer score: 19

Revisions (0)

No revisions yet.