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" .