I'm currently facing an issue with accessing request parameters in my express router.
In my server.js
file, I have the following setup:
app.use('/user/:id/profile', require('./routes/profile'));
Within my ./routes/profile.js
file, this code is present:
router.get('/', (req, res) => {
console.log(req.params.id);
}
Despite this setup, the console log displays undefined
.
I am fairly new to express and suspect that I might be overlooking something fundamental about how routing functions.
Could someone kindly provide some guidance on this?
Below is the complete content of my server.js
:
const express = require('express');
const app = express();
app.use(express.json());
app.use('/user/:id/profile', require('./routes/profile'));
app.listen(5000, () => console.log('Listening'));
Below is the full content of my profile.js
:
const express = require('express');
const router = express.Router();
router.get('/', (req, res) => {
console.log(req.params.id);
res.status(200).send('In profile route');
});
module.exports = router;