繁体   English   中英

带有参数的节点js路由器404

[英]Node js router 404 with params

我想将参数传递给页面。 但是有404。我的代码:
app.js

var routes = require('./routes/index');
var app = express();

路由/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});
});

我尝试http:// localhost:3000 / profile?id = 56e2c3c2cdde3f64302ac154遇到错误:找不到

您的路线应如下所示:

 http://localhost:3000/profile/56e2c3c2cdde3f64302ac154

它是自动设置的req.params.id

路径参数和查询参数之间有区别。 您定义的网址

/profile/:id

说到路由框架,我希望id为Path参数,即资源路径的一部分。 但是您在url请求中

 http://localhost:3000/profile?id=56e2c3c2cdde3f64302ac154

您正在发送ID作为查询参数。 因此,路由框架无法识别以id为查询参数的url。 因此,它返回404,表示“服务器找不到所请求的内容”。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM