I am using current express 4 and node.js 6.x. In my web application, I need to do an internal redirection (i.e. redirect from one route handler to another without sending the redirection to the client). This works on the same level or inside the same router, but not if the source request is in the router.
See the following minimum working example:
const express = require("express");
const app = module.exports.app = express();
const router = new express.Router();
app.use("/myrouter", router);
// not working
router.get("/test", (req, res, next) => {
req.url = "/toplevel";
next();
});
// working
app.get("/test", (req, res, next) => {
req.url = "/toplevel";
next();
});
// debug output
app.use((req, res, next) => {
console.log(`request to ${req.url}`);
next();
});
app.get("/toplevel", (req, res) => {
res.end("toplevel");
});
app.listen(8000);
" http://localhost:8000/test", "toplevel", " / ". , http://localhost:8000/myrouter/test ". " /myrouter/toplevel ", -404 .
"/myrouter/test" "/toplevel" (.. res.redirect())?