[英]Express.js 4 routes not matching with router.route
I'm trying to get the hang of router.route in Express 4. The docs make it sound awesome, but it's just not working for me.我正在尝试掌握 Express 4 中 router.route 的窍门。文档使它听起来很棒,但它对我不起作用。
If I use the command line tool to make a standard app and then add routes/contacts.js that looks like this:如果我使用命令行工具制作标准应用程序,然后添加如下所示的 routes/contacts.js:
var express = require('express');
var router = express.Router();
router.route('/:contactid')
.get(function(req, res) {
res.send('(get) It worked '+contactid);
})
module.exports = router;
Then in app.js add:然后在 app.js 添加:
var contacts = require('./routes/contacts');
...
app.use('/contacts', contacts);
I'd expect http://localhost:8000/contacts/1
to match the route from contacts.js.我希望
http://localhost:8000/contacts/1
与来自 contacts.js 的路由相匹配。 However, I get an error that essentially indicates it's not matching any routes in contacts.js但是,我收到一个错误,基本上表明它不匹配 contacts.js 中的任何路由
Error: Not Found
at Layer.app.use.res.render.message [as handle] (project1/app.js:31:15)
at trim_prefix (project1/node_modules/express/lib/router/index.js:226:17)
at c (project1/node_modules/express/lib/router/index.js:198:9)
at Function.proto.process_params (project1/node_modules/express/lib/router/index.js:251:12)
at next (project1/node_modules/express/lib/router/index.js:189:19)
at next (project1/node_modules/express/lib/router/index.js:150:14)
at next (project1/node_modules/express/lib/router/index.js:166:38)
at Function.proto.handle (project1/node_modules/express/lib/router/index.js:234:5)
at Layer.router (project1/node_modules/express/lib/router/index.js:23:12)
at trim_prefix (project1/node_modules/express/lib/router/index.js:226:17)
If I add routes using a static prefix, it works as expected:如果我使用 static 前缀添加路由,它会按预期工作:
router.get('/1', function(req, res) {
res.send('It worked!');
});
// http://localhost:8000/contacts/1 says "It worked!"
Any tips on what I'm doing wrong?关于我做错了什么的任何提示?
Router paths are relative to the mounted path. 路由器路径是相对于安装路径的。 So your contacts router would instead just be:
因此,您的联系路由器将只是:
router.route('/:contactid')
.get(function(req, res) {
res.send('(get) It worked ' + req.params.contactid);
})
I think this should work (does for me) 我认为这应该工作(对我有用)
In routes/contacts.js 在route / contacts.js中
/* Created by matthias on 6/9/14. */
var express = require('express');
var router = express.Router();
router.get('/:contactid', function(req, res) {
res.send('(get) It worked ' + req.params.contactid);
});
module.exports = router;
Then in app.js 然后在app.js中
var contacts = require('./routes/contacts');
var app = express();
app.use('/contacts', contacts);
Works for me: localhost:3000/contacts/:3 对我有用:localhost:3000 / contacts /:3
Predictably getting: (get) It worked 3 可以预期地得到:(获取)成功了3
Saved me a lot of headaches.... Finally understood that app.js is MOUNTING the route before and the susbsequent middleware needs to be be RELATIVE to that mounting point.帮我省了很多麻烦……终于明白了 app.js 之前正在安装路由,随后的中间件需要与该安装点相关。
I wish express documentation showed that clearly我希望快递文件清楚地表明
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.