Wiggles
Wiggles

Reputation: 143

Trouble organizing expressjs routes

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();
    });

};

Upvotes: 1

Views: 867

Answers (2)

Wiggles
Wiggles

Reputation: 143

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();
    });

};

Upvotes: 2

srquinn
srquinn

Reputation: 10481

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. =)

Upvotes: 2

Related Questions