admin管理员组文章数量:1391995
I wanna pass parameter to page. But have 404. My code:
app.js
var routes = require('./routes/index');
var app = express();
routes/index.js:
var express = require('express');
var router = express.Router();
router.get('/profile/:id', function (req, res) {
var id = req.params.id;
console.log(id);
res.render('profile', {id: id});
});
and i try http://localhost:3000/profile?id=56e2c3c2cdde3f64302ac154 but have Error: Not Found
I wanna pass parameter to page. But have 404. My code:
app.js
var routes = require('./routes/index');
var app = express();
routes/index.js:
var express = require('express');
var router = express.Router();
router.get('/profile/:id', function (req, res) {
var id = req.params.id;
console.log(id);
res.render('profile', {id: id});
});
and i try http://localhost:3000/profile?id=56e2c3c2cdde3f64302ac154 but have Error: Not Found
Share Improve this question asked Mar 12, 2016 at 8:39 NickDevilNickDevil 1854 silver badges14 bronze badges2 Answers
Reset to default 6Your route should be looks like:
http://localhost:3000/profile/56e2c3c2cdde3f64302ac154
It is automaticaly set req.params.id
.
There is a difference between Path param and Query param . The Url you have defined
/profile/:id
Says to the routing framework that , I expect id as Path param i.e part of the resource path . But in the url request you made
http://localhost:3000/profile?id=56e2c3c2cdde3f64302ac154
You are sending id as query param . So the routing framework is unaware of the url with id as a query param . Hence it returns a 404 meaning "the server could not find what was requested" .
本文标签: javascriptNode js router 404 with paramsStack Overflow
版权声明:本文标题:javascript - Node js router 404 with params - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744577289a2613703.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论