Forward request between router in expressjs












0














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?










share|improve this question
























  • What exactly are you trying to accomplish? What does ItemRouter.get() do that requires forwarding the request to another router? next('route') is typically meant to be used in middleware chains, not to forward requests to other routers (I don't even think it's possible to use it for that purpose, to be honest).
    – robertklep
    Dec 3 at 11:52










  • We have a service, and we need to provide different endpoints according to different specification based on the same service. Of course we can call the common/shared components in different routers for different specification. But we think forward is better. Since in my opinion, different specification endpoints are just wrappers.
    – hguser
    Dec 4 at 2:07










  • I don't agree that forwarding is the correct solution. It obfuscates the request flow (if an outsider would read next('route'), they have no idea where the request is being forwarded to because it's highly unspecific), is prone to bugs (what if you introduce another router into the mix?), and next('route') isn't meant to be used like that. I think shared components, or middleware, is a better solution.
    – robertklep
    Dec 4 at 6:57












  • I agree that next('route') is a bad idea. In fact I prefer the manner in jsp like forward:/path which will make it clear enough that where this request will be forwarded.
    – hguser
    Dec 4 at 8:52
















0














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?










share|improve this question
























  • What exactly are you trying to accomplish? What does ItemRouter.get() do that requires forwarding the request to another router? next('route') is typically meant to be used in middleware chains, not to forward requests to other routers (I don't even think it's possible to use it for that purpose, to be honest).
    – robertklep
    Dec 3 at 11:52










  • We have a service, and we need to provide different endpoints according to different specification based on the same service. Of course we can call the common/shared components in different routers for different specification. But we think forward is better. Since in my opinion, different specification endpoints are just wrappers.
    – hguser
    Dec 4 at 2:07










  • I don't agree that forwarding is the correct solution. It obfuscates the request flow (if an outsider would read next('route'), they have no idea where the request is being forwarded to because it's highly unspecific), is prone to bugs (what if you introduce another router into the mix?), and next('route') isn't meant to be used like that. I think shared components, or middleware, is a better solution.
    – robertklep
    Dec 4 at 6:57












  • I agree that next('route') is a bad idea. In fact I prefer the manner in jsp like forward:/path which will make it clear enough that where this request will be forwarded.
    – hguser
    Dec 4 at 8:52














0












0








0







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?










share|improve this question















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?







node.js express






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Dec 3 at 11:14

























asked Nov 21 at 3:20









hguser

12.2k38126234




12.2k38126234












  • What exactly are you trying to accomplish? What does ItemRouter.get() do that requires forwarding the request to another router? next('route') is typically meant to be used in middleware chains, not to forward requests to other routers (I don't even think it's possible to use it for that purpose, to be honest).
    – robertklep
    Dec 3 at 11:52










  • We have a service, and we need to provide different endpoints according to different specification based on the same service. Of course we can call the common/shared components in different routers for different specification. But we think forward is better. Since in my opinion, different specification endpoints are just wrappers.
    – hguser
    Dec 4 at 2:07










  • I don't agree that forwarding is the correct solution. It obfuscates the request flow (if an outsider would read next('route'), they have no idea where the request is being forwarded to because it's highly unspecific), is prone to bugs (what if you introduce another router into the mix?), and next('route') isn't meant to be used like that. I think shared components, or middleware, is a better solution.
    – robertklep
    Dec 4 at 6:57












  • I agree that next('route') is a bad idea. In fact I prefer the manner in jsp like forward:/path which will make it clear enough that where this request will be forwarded.
    – hguser
    Dec 4 at 8:52


















  • What exactly are you trying to accomplish? What does ItemRouter.get() do that requires forwarding the request to another router? next('route') is typically meant to be used in middleware chains, not to forward requests to other routers (I don't even think it's possible to use it for that purpose, to be honest).
    – robertklep
    Dec 3 at 11:52










  • We have a service, and we need to provide different endpoints according to different specification based on the same service. Of course we can call the common/shared components in different routers for different specification. But we think forward is better. Since in my opinion, different specification endpoints are just wrappers.
    – hguser
    Dec 4 at 2:07










  • I don't agree that forwarding is the correct solution. It obfuscates the request flow (if an outsider would read next('route'), they have no idea where the request is being forwarded to because it's highly unspecific), is prone to bugs (what if you introduce another router into the mix?), and next('route') isn't meant to be used like that. I think shared components, or middleware, is a better solution.
    – robertklep
    Dec 4 at 6:57












  • I agree that next('route') is a bad idea. In fact I prefer the manner in jsp like forward:/path which will make it clear enough that where this request will be forwarded.
    – hguser
    Dec 4 at 8:52
















What exactly are you trying to accomplish? What does ItemRouter.get() do that requires forwarding the request to another router? next('route') is typically meant to be used in middleware chains, not to forward requests to other routers (I don't even think it's possible to use it for that purpose, to be honest).
– robertklep
Dec 3 at 11:52




What exactly are you trying to accomplish? What does ItemRouter.get() do that requires forwarding the request to another router? next('route') is typically meant to be used in middleware chains, not to forward requests to other routers (I don't even think it's possible to use it for that purpose, to be honest).
– robertklep
Dec 3 at 11:52












We have a service, and we need to provide different endpoints according to different specification based on the same service. Of course we can call the common/shared components in different routers for different specification. But we think forward is better. Since in my opinion, different specification endpoints are just wrappers.
– hguser
Dec 4 at 2:07




We have a service, and we need to provide different endpoints according to different specification based on the same service. Of course we can call the common/shared components in different routers for different specification. But we think forward is better. Since in my opinion, different specification endpoints are just wrappers.
– hguser
Dec 4 at 2:07












I don't agree that forwarding is the correct solution. It obfuscates the request flow (if an outsider would read next('route'), they have no idea where the request is being forwarded to because it's highly unspecific), is prone to bugs (what if you introduce another router into the mix?), and next('route') isn't meant to be used like that. I think shared components, or middleware, is a better solution.
– robertklep
Dec 4 at 6:57






I don't agree that forwarding is the correct solution. It obfuscates the request flow (if an outsider would read next('route'), they have no idea where the request is being forwarded to because it's highly unspecific), is prone to bugs (what if you introduce another router into the mix?), and next('route') isn't meant to be used like that. I think shared components, or middleware, is a better solution.
– robertklep
Dec 4 at 6:57














I agree that next('route') is a bad idea. In fact I prefer the manner in jsp like forward:/path which will make it clear enough that where this request will be forwarded.
– hguser
Dec 4 at 8:52




I agree that next('route') is a bad idea. In fact I prefer the manner in jsp like forward:/path which will make it clear enough that where this request will be forwarded.
– hguser
Dec 4 at 8:52

















active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53404804%2fforward-request-between-router-in-expressjs%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown






























active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53404804%2fforward-request-between-router-in-expressjs%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

404 Error Contact Form 7 ajax form submitting

How to know if a Active Directory user can login interactively

TypeError: fit_transform() missing 1 required positional argument: 'X'