Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Node js router 404 with params

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

like image 753
NickDevil Avatar asked Jun 07 '26 01:06

NickDevil


2 Answers

Your route should be looks like:

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

It is automaticaly set req.params.id.

like image 77
Dmitriy Avatar answered Jun 09 '26 15:06

Dmitriy


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

like image 38
Raghavan Avatar answered Jun 09 '26 15:06

Raghavan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!