Node.js Routing – Purpose of Separating Routes into Individual Modules

code-organizationnode.jsrouting

My app uses Node.js and Express 4 and has the following code in the app.js file located in the root directory:

var express = require('express');

var index = require('./routes/index');
var users = require('./routes/users');

var app = express();

app.use('/', index);
app.use('/users', users);

The routes folder in the root directory contains an index.js file with the following code:

var express = require('express');
var router = express.Router();

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

module.exports = router;

The routes folder also contains a users.js file with the following code:

var express = require('express');
var router = express.Router();

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

module.exports = router;

I'm new to Express and Node.js. It's my understanding that this structure causes browser requests to domainName.com to run the index.js file, while requests to domainName.com/users will run the users.js file, resulting in different views being rendered in the browser.

What is the purpose of organizing the code this way? (i.e. why is app.use() in app.js used to isolate each router.get(path, callback) into it's own respective module?)

Is the commonly-accepted practice to organize routes differently for a single page app versus a website consisting of mostly static text, such as a blog? If so, how?

Best Answer

When you only have two routes, it makes no sense to separate them in this way. However, when you consider a real application with many routes, it begins to make more sense.

/routes/user.js

var express = require('express');
var router = express.Router();
var User = require('../models/user');

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

router.param('userid', function (req, res, next, userid) {
    req.user = User.findById(userid);
    req.userid = userid;
});

router.get('/:userid', function (req, res, next) {
    req.user.exec(function (err, user) {
        if (err) { return next(err); }
        res.render('user', {user: user});
    });
});

router.put('/:userid', function (req, res, next) {
    User.findByIdAndUpdate(req.userid, req.body, function (err) {
        if (err) { return next(err); }
        res.render('user-updated');
    });
});

router.delete('/:userid', function (req, res, next) {
    User.findByIdAndDelete(req.userid, function (err) {
        if (err) { return next(err); }
        res.render('user-deleted');
    });
});

module.exports = router;

Having all of that in app.js will get out of hand very quickly, even more so once you have more models than just user.

Also note that this isn't the only way to organize your routes/models/views nor is it the "best" way for all cases. You'll have you decide for yourself what will work best in each application.


Note, the code in the sample is of course all just sample code that probably won't work. My model methods were based off of mongoose model methods.

Related Topic