0

So as the title says. I need to call a route from another route.

I have used run-middleware npm module but after using it Morgan showing odd outputs, like showing two http request but both are same. Also accessing invalid route not going to catch all route. Instead its throwing error. Here is a log.

data:    app/server.js:29146 - ======================================
data:    app/server.js:29146 - 200
data:    app/server.js:29146 - <!DOCTYPE html>
data:    app/server.js:29146 - <html lang="en">
data:    app/server.js:29146 - <head>
data:    app/server.js:29146 - <meta charset="utf-8">
data:    app/server.js:29146 - <title>Error</title>
data:    app/server.js:29146 - </head>
data:    app/server.js:29146 - <body>
data:    app/server.js:29146 - <pre>Cannot POST /api/v1/auth/authenticate</pre>
data:    app/server.js:29146 - </body>
data:    app/server.js:29146 - </html>
data:    app/server.js:29146 - ======================================
data:    app/server.js:29146 - POST /api/v1/auth/authenticate - - ms - -
data:    app/server.js:29146 - POST /api/v1/auth/authenticate 200 474.491 ms - 268

I used the below code where /asd route is invalid

req.runMiddleware('/asd',{method:'post'},function(code,data){
    console.log('======================================');
    console.log(code);
    console.log(data);
    console.log('======================================');
});

Is there any other library for this purpose?

or anyone can help me to create my own? I know how to make a middleware & how to invoke the middleware chain of express, I just want to know which fields of req objects need to be changed and restore.

Jayadratha Mondal
  • 759
  • 1
  • 10
  • 21

1 Answers1

0

I cannot really tell exactly what you're asking because you don't show the code where you're trying to call another route.

If you are in one route handler and you want to call the logic of another route handler, there are several options:

  1. Extract the common code into a function that both route handlers can execute. Thus, you can call the common code from either route handler. For some reason, people seem to forget that route handlers can be factored into shared code just like all other Javascript functions.

  2. Return a 302 redirect to tell the caller to redirect to another route. It will then request that other route and that route handler can handle things.

  3. Make an http request to your own server to get the result of the other route and then use that result in this route.

Usually option 1 is the best way to go since it involves the least overhead.

You really don't want to go the route of trying to make up your own req and res objects that you can trick express into doing something for you. Just take express out of the equation entirely and factor the shared code into a separate function that is independent of req and res. Pass in the required inputs to the shared function and return out the result, then have each route separately deal with their own req and res. Or, if you want them to both operate identically on req and res, you can pass those directly into the shared function and let it operate on them directly.

jfriend00
  • 683,504
  • 96
  • 985
  • 979
  • Currently I have Controllers which returns promise and they are independent of req and res. But now having an issue where there has a lot of .then inside the controller, sometime I need to skip some .then after one & also return value to the caller of the Controller.. Do you know how to skip .then other than throwing a error? – Jayadratha Mondal Sep 27 '17 at 20:54
  • @JayadrathaMondal - We really can't help you very well with these kind of theoretical questions. Show us the actual code (in your question) and I'm quite sure we can offer a very specific and useful suggestion. That also sounds like a different question from what you first posted (since it's about the controller, so maybe you should post it (with appropriate code segments) in a new question. If you post a comment here with a link to the new question, I will take a look at the new question. – jfriend00 Sep 27 '17 at 21:12
  • yes the last comment is not related to the question. I will make a new question if I completely fail to follow your option 1. – Jayadratha Mondal Sep 27 '17 at 21:25