最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Express "Cannot GET query" error - Stack Overflow

programmeradmin1浏览0评论

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?

Share Improve this question asked Aug 4, 2012 at 10:56 GntemGntem 7,1652 gold badges37 silver badges48 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 5

look 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);
}
发布评论

评论列表(0)

  1. 暂无评论