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