javascript - 表达 "Cannot GET/query"错误

标签 javascript node.js express routes

我最近尝试使用 Express,我发现它有点困难,我尝试在 app.js 文件中定义路由,然后在 requireindex.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 尝试了这个例子,我也得到了一个错误
为什么我不能让它工作?

最佳答案

仔细观察示例:Line 7Line 21 .

应用程序.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/

相关文章:

javascript - 如何简化动画 jQuery 代码

node.js - 将图像位图传递给 Azure Face SDK detectorWithStream()

angularjs - Angular $scope 数据不会更新

javascript - 在 Sails.JS 中获取上传图像的下载 url

node.js - 检查用户是否通过 Passport 策略登录(不同用户类型)

javascript - 如何将 : Pass node. js mongodb 结果转换为 angularjs?

javascript - 如何定位数据表导出按钮组?

javascript - 是否可以使用 material ui 选项卡并仍然使用 react router?

javascript - Angular JS 与快速联系表单返回 404

javascript - mongoDB 通过查找获取 'undefined'