2

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?

  • 1
    I've removed your references to "best practices," because you didn't define what you meant by "best" in a way that is relevant to your specific design issues. See also meta.stackexchange.com/questions/142353/… – Robert Harvey Apr 15 '15 at 17:57
  • In any event, the organization of routes and modules in this fashion is simply that: organization. – Robert Harvey Apr 15 '15 at 17:59
  • @RobertHarvey Why couldn't the 'router.get(path, callback)' just be included in the 'app.js' file? What is the benefit of separating that portion of the code into separate modules? – Jackmc1047 Apr 15 '15 at 18:17
  • It's the same benefit that you would get by organizing any other system into separate modules. If you don't believe this organization benefits you, then by all means: use one giant module for everything. – Robert Harvey Apr 15 '15 at 21:43
  • @Jackmc1047 I think they're expecting a real project to add significantly more code to each of those files. – Ixrec Apr 15 '15 at 21:45
1

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.

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.