1

I am following along with this article, which describes a good way to organize routes in express. I am encountering a problem though when i try to access the functions that i have exported from my main.js file. I get a 404 error when i curl "localhost/user/username"

//the routes section of my my app.js file
app.get('/', routes.index);
app.get('/user/:username', routes.getUser);

//my index.js file
require('./main');
require('./users');

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

//my main.js file

exports.getUser = function(req, res){
        console.log('this is getUser');
        res.end();
};

----EDITED WITH MY SOLUTION----

Here is the solution that I went with, maybe someone will find it useful. I'm also open to hearing suggestions about whether or not this is going to cause me any problems in the future.

//-------The routes in my app.js file now look like this.

require('./routes/index')(app);
require('./routes/main')(app);

//-------In index.js i now have this

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

//-------My main.js now looks like this-------

module.exports = function(app){

    app.get('/user/:username', function(req, res){
            var crawlUser = require('../engine/crawlUser');
            var username = req.params.username;
            crawlUser(username);
            res.end();
    });

};
Wiggles
  • 143
  • 8
  • 1
    you should post an answer an accept your own answer. That way people will know the question has been solved and they needn't spend time on it. – Peter Lyons Dec 05 '12 at 05:41
  • You solution looks fine to me. It looks a lot like what I describe here: http://stackoverflow.com/questions/5778245/expressjs-how-to-structure-an-application/7350875#7350875 – Peter Lyons Dec 05 '12 at 05:42

2 Answers2

2

Globals are evil and should be avoided at all costs. Here is how I organize my routes without globals and without excessive boiler plate code.

// File Structure

/app.js
/routes
/--index.js
/--main.js
/--users.js

// app.js
var app = require('express');

/* Call Middleware here */

require('routes')(app);
app.listen(3000);

---------------------------------------------

// routes/index.js - This is where I store all my route definitions
// in a long list organized by comments. Allows you to only need to go to
// one place to edit route definitions. 

module.exports = function(app) {

  var main = require('./main');
  app.get('/', main.get);

  var users = require('./users');
  app.get('/users/:param', users.get);

  ...

}

---------------------------------------------

// routes/main.js - Then in each submodule you define each function and attach
// to exports

exports.get = function(req, res, next){
  // Do stuff here
})

I guess in the end its a matter of preference, but if you want your code to maintain agility and work with other modules you should avoid global variables. Even if Alex Young says its ok. =)

srquinn
  • 10,134
  • 2
  • 48
  • 54
  • Thank you for taking the time to respond to my question. I decided on a different approach which i have included in my original post if your interested. @jibsales – Wiggles Dec 03 '12 at 02:00
  • That's basically the same thing I suggested with the only difference being I choose to only mount one route module so I don't have to pass the app object multiple times. Your way is fine when you have a small number of routes. I have over 50 routes in my project and its becomes a boilerplate mess if I don't do it this way. – srquinn Dec 03 '12 at 06:55
2

Here is the solution that I went with, maybe someone will find it useful.

//-------The routes in my app.js file now look like this.

require('./routes/index')(app);
require('./routes/main')(app);

//-------In index.js i now have this

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

//-------My main.js now looks like this-------

module.exports = function(app){

    app.get('/user/:username', function(req, res){
            var crawlUser = require('../engine/crawlUser');
            var username = req.params.username;
            crawlUser(username);
            res.end();
    });

};
Wiggles
  • 143
  • 8