我最近尝试使用 Express,我发现它有点困难,我尝试在 app.js
文件中定义路由,然后在 require
到 index.js
,当我尝试浏览到 localhost:3000/route
query.js 文件
exports.show = function(reg,res){
res.render("test",{title:"query testing"});
};
我在 app.js
中尝试过这个
app.get('/query',require('./routes/query.js'));
并且在 index.js
var queryX = require('./query.js');
app.get('/query',queryX.show);
我在 github 上用 route-separation
尝试了这个例子,我也得到了一个错误
为什么我不能让它工作?
最佳答案
应用程序.js:
var site = require('./routes/site.js');
app.get('/', site.index);
routes/site.js:
module.exports = function(req, res) { ... };
如果你想使用 routes/index.js 来存储你的所有路线,你必须将 app
传递给导出的函数。
类似于:
应用程序.js:
var express = require('../..')
, app = express();
require('./routes')(app);
路由/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);
}
关于javascript - 表达 "Cannot GET/query"错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11808080/