16
\$\begingroup\$

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.

\$\endgroup\$
  • \$\begingroup\$ Shouldn't it be res.send({ some: 'json' }); \$\endgroup\$ – user59388 Dec 3 '14 at 21:36
  • \$\begingroup\$ thats just an explanation what to do. In most cases it would be res.send(someData) where someData is variable where json is stored. In real world You almost never send back json composed inside res.send() method and i dont want to encourage or suggest that. \$\endgroup\$ – Jarema Dec 4 '14 at 8:50
  • \$\begingroup\$ it would be nice to see the file names as well. a little stuck here :( @Jarema \$\endgroup\$ – Val Feb 22 '15 at 14:23
18
\$\begingroup\$

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;    
})();
|improve this answer|||||
\$\endgroup\$
  • \$\begingroup\$ thanks! that looks good. Most important part is that about caching modules. good point! I will look into Your idea of exported routes little later this day and then accept answer :). \$\endgroup\$ – Jarema Jun 4 '14 at 10:04
  • \$\begingroup\$ Why do you use immediately invoked functions to return the value of module.exports? Can't you just export the router? \$\endgroup\$ – kmiyashiro Feb 17 '15 at 7:46
  • \$\begingroup\$ 'use strict' maybe ? Otherwise, there is no reason. I don't quite remember. Sorry. \$\endgroup\$ – jackdbernier Feb 18 '15 at 6:34
0
\$\begingroup\$

As none answered, and I did some research and debugging:

All app routes acts like middleware - so you can pass app and express into their files many times, but bear in mind, that in express 4 ORDER OF MIDDLEWARES IS IMPORTANT. Don't forget this while creating our exports (for ie, don't export first route that have app.get('*'), because all the rest like /api and /partials will not work). Remember about possible conflict in same way as in "typical situation".

|improve this answer|||||
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.