i recently tried to work with Express and i find it kind of difficult, i tried defining routes in app.js
file and after require
to index.js
and i still get this error when i try to browse to localhost:3000/route
the query.js file
exports.show = function(reg,res){
res.render("test",{title:"query testing"});
};
i tried this in app.js
app.get('/query',require('./routes/query.js'));
and in index.js
var queryX = require('./query.js');
app.get('/query',queryX.show);
i tried the example with route-separation
on github and i get an error to that too
why i can't get this to work?
i recently tried to work with Express and i find it kind of difficult, i tried defining routes in app.js
file and after require
to index.js
and i still get this error when i try to browse to localhost:3000/route
the query.js file
exports.show = function(reg,res){
res.render("test",{title:"query testing"});
};
i tried this in app.js
app.get('/query',require('./routes/query.js'));
and in index.js
var queryX = require('./query.js');
app.get('/query',queryX.show);
i tried the example with route-separation
on github and i get an error to that too
why i can't get this to work?
1 Answer
Reset to default 5look closely in the example: Line 7 and Line 21.
app.js:
var site = require('./routes/site.js');
app.get('/', site.index);
routes/site.js:
module.exports = function(req, res) { ... };
If you want to use routes/index.js to store all your routes you'll have to pass app
to an exported function.
something like:
app.js:
var express = require('../..')
, app = express();
require('./routes')(app);
routes/index.js:
var more_routes = require('./more_routes');
module.exports = function(app) {
app.get('/', function(req, res){...});
app.get('/show', more_routes.show);
app.get('/list', more_routes.list);
}