Using routes in Express-js Using routes in Express-js express express

Using routes in Express-js


So, after I created my question, I got this related list on the right with a similar issue: Organize routes in Node.js.

The answer in that post linked to the Express repo on GitHub and suggests to look at the 'route-separation' example.

This helped me change my code, and I now have it working. - Thanks for your comments.

My implementation ended up looking like this;

I require my routes in the app.js:

var express = require('express')  , site = require('./site')  , wiki = require('./wiki');

And I add my routes like this:

app.get('/', site.index);app.get('/wiki/:id', wiki.show);app.get('/wiki/:id/edit', wiki.edit);

I have two files called wiki.js and site.js in the root of my app, containing this:

exports.edit = function(req, res) {    var wiki_entry = req.params.id;    res.render('wiki/edit', {        title: 'Editing Wiki',        wiki: wiki_entry    })}


The route-map express example matches url paths with objects which in turn matches http verbs with functions. This lays the routing out in a tree, which is concise and easy to read. The apps's entities are also written as objects with the functions as enclosed methods.

var express = require('../../lib/express')  , verbose = process.env.NODE_ENV != 'test'  , app = module.exports = express();app.map = function(a, route){  route = route || '';  for (var key in a) {    switch (typeof a[key]) {      // { '/path': { ... }}      case 'object':        app.map(a[key], route + key);        break;      // get: function(){ ... }      case 'function':        if (verbose) console.log('%s %s', key, route);        app[key](route, a[key]);        break;    }  }};var users = {  list: function(req, res){    res.send('user list');  },  get: function(req, res){    res.send('user ' + req.params.uid);  },  del: function(req, res){    res.send('delete users');  }};var pets = {  list: function(req, res){    res.send('user ' + req.params.uid + '\'s pets');  },  del: function(req, res){    res.send('delete ' + req.params.uid + '\'s pet ' + req.params.pid);  }};app.map({  '/users': {    get: users.list,    del: users.del,    '/:uid': {      get: users.get,      '/pets': {        get: pets.list,        '/:pid': {          del: pets.del        }      }    }  }});app.listen(3000);


Seems that only index.js get loaded when you require("./routes") .I used the following code in index.js to load the rest of the routes:

var fs = require('fs')  , path = require('path');fs.readdirSync(__dirname).forEach(function(file){  var route_fname = __dirname + '/' + file;  var route_name = path.basename(route_fname, '.js');  if(route_name !== 'index' && route_name[0] !== "."){     exports[route_name] = require(route_fname)[route_name];  }});