0

I want some help to build a structure for my node.js application.

My requirement is:

Req R1 will come to say endpoint E1, with a payload P1 (having callback url (say U1) as well), I will validate R1 and if it pass some of the basic validations I have to make a new "POST" req R2 on some predefined URL U2 and send response RS1 cooresponding to R1. But I don't want to wait for response from R2 (I just want to POST the request if R1 is valid). If R1 is invalid, I don't want to POST the request R2; in any case, i have to send response to R1.

I know, it's might be too long, but I just want help in choosing the correct approach.

Abhijeet Ahuja
  • 5,596
  • 5
  • 42
  • 50
  • This isn't a structure question. It's just a series of steps to be implemented one after another. What kind of answer are you expecting here? Start with implementing the first step in your scheme, then implement the second step and so on. Or, ask a much more specific question. – jfriend00 Jul 08 '16 at 04:12
  • It's not clear what kind of answer you are asking for, but the most non-trivial part of this I can see is actually extracting the payload from R1. You don't specify what kind of request R1 is or how the payload P1 is being delivered. Assuming it's a post request, you can use use the `bodyParser` middleware. Some of that is covered in this question and its answers: http://stackoverflow.com/questions/4295782/how-do-you-extract-post-data-in-node-js – Greg Nisbet Jul 08 '16 at 04:17
  • I just want to trigger another request R2 (conditional) and send response to R1 in any case, and don't expect response from R2 – Abhijeet Ahuja Jul 08 '16 at 04:38
  • So, if you aren't going to validate the contents of `R1` at all, then you'd do something like `app.post("/", function (req, res) { send_request_to_U2(some_callback); res.send(whatever);)})` Point being that `res.send(whatever)` will get executed right after R1 reaches E1. If you wanted to delay `res.send(whatever)` until after the response gets back from E2, then you would need to place it in the body of `some_callback`. – Greg Nisbet Jul 08 '16 at 04:55
  • @Grogory - you almost got me, but I don't want to wait for response from E2, i just want to trigger E2, and close the http request R2. – Abhijeet Ahuja Jul 08 '16 at 05:01

0 Answers0