'Express route with dynamic params not working

router.get('/:id ', (req, res) => {
    res.send(req.params.id);
});

When I call this on exmaple "http://localhost/12" I will get an error called "Cannot Get /12"

Any ideas what I am missing out?



Solution 1:[1]

You need to register your router to main app in your index.js

In your router.js folder

const express = require('express');
const router = express.Router()
router.get('/:id ', (req, res) => {
    res.send(req.params.id);
});

and in your index.js

const express = require('express');
app = express()
app.use(router)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 IT ACADEMY