Node.js / Express routing -
i'm new express. way i'm doing routing kicking error.
here relevant code:
app.js
var express = require('express') , routes = require('./routes') , http = require('http') , path = require('path') , firebase = require('firebase'); ... // routing app.get('/', routes.index); app.get('/play', routes.play); index.js , play.js
exports.index = function(req, res){ res.sendfile('views/index.html'); }; exports.play = function(req, res){ res.sendfile('views/play.html'); }; this error:
error: .get() requires callback functions got [object undefined]
it references line in app.js
app.get('/play', routes.play); i'm lost why doesnt work because code structure identical routing index page , index page loads perfectly.
any ideas? thanks
the issue routes.play undefined when function expected.
console.log(typeof routes.play); // ... if routes split multiple files @ least comment, "index.js , play.js," suggests:
// routes/index.js exports.index = function(req, res){ res.sendfile('views/index.html'); }; // routes/play.js exports.play = function(req, res){ res.sendfile('views/play.html'); }; requiring directory include index.js. so, you'll still need require('./play') somewhere.
you can either "forward" within
index.js:exports.index = function(req, res){ res.sendfile('views/index.html'); }; var playroutes = require('./play'); exports.play = playroutes.play;alternatively:
exports.play = require('./play');app.get('/play', routes.play.play);or require directly in
app.jswell:var express = require('express') , routesindex = require('./routes') , routesplay = require('./routes/play') // ... // routing app.get('/', routesindex.index); app.get('/play', routesplay.play);
Comments
Post a Comment