I have come across several Stack Overflow posts suggesting that to rewrite a URL in Express 4, one would need to implement something similar to the code below:
router.use('/one/:someId', (req, res, next) => {
req.url = `/two/${req.params.someId}`;
next();
});
router.get('/one/:someId', (req, res) => {
res.send("reached /one/:someId");
});
router.get('/two/:someId', (req, res) => {
res.send("reached /two/:someId");
});
However, upon testing this approach, not only does the URL fail to change as expected to "/two/some integer", but it also leads to a 404 - Not Found error page that I have configured in my application file.
These routes are located in a separate router file, and I even attempted setting the URL to:
req.url = `/routerPath/two/${req.params.someId}`;
but the outcome remains unchanged.
What could I possibly be overlooking?
Appreciate any insights you can offer.