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 badges
Add a ment  | 

2 Answers 2

Reset to default 6

Your 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