I have multiple routers when using express(4.16.4) like this:
var app=express();
app.use("/item",ItemRouter)
app.use("/user",UserRouter)
.....
And I want to forward(not the client redirect) the request from ItemRouter
to UserRouter
:
ItemRouter.js:
var ItemRouter = express.Router();
ItemRouter.get("/xxx",(req,res,next)=>{
// jump to /user/xxx
next("route")
});
I register the ItemRouter
before UserRouter
. And The usage of next('route')
is got by searching in google and sf. For example:
Forward request to alternate request handler instead of redirect
This question meet the same problem, and there is an answser suggested three options:
Option 1: route multiple paths to the same handler function
Option 2: Invoke a separate handler function manually/conditionally
Option 3: call next('route')
And I prefer to option 3, since in my opinion each router should be separated and de-coupled.
However as shown, it does not work.
Did I miss anything?