crankshaft
crankshaft

Reputation: 2677

Node Express Routing

I am struggling to understand the routing for express / jade, all of the examples I have seen are for only 1 page index.html, however what if you site has more than 1 page ?

in my /routes folder I have the following in the index.js file:

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

exports.sched = function(req, res){
  res.render('sched.jade', { title: 'Schedules' });
};

However, the sched.jade page is not rendered when I attempt to open the url: /sched

What am I doing wrong ??

Upvotes: 0

Views: 307

Answers (2)

John Steel
John Steel

Reputation: 324

If you are working from a template you might have a file called 'boot.js', mine is in a lib folder. This file has all of the routing information. My routing has a switch that looks like this:

for (var key in obj) {
switch (key) {
    case 'show':
      method = 'get';
      path = '/' + name + '/:' + name + '_id';
      break;
    case 'index':
      method = 'get';
      path = '/';
      break;
    case 'services':
      method = 'get';
      path = '/services';
      break;
    default:
      throw new Error('unrecognized route: ' + name + '.' + key);
  }

Upvotes: 0

arknave
arknave

Reputation: 613

In the file you call node on (usually app.js or server.js), you need to import the routes and then call the functions. For example:

var routes = require('routes/');

app.get('/sched', routes.sched);
app.get('/', routes.index);

Upvotes: 1

Related Questions