have my express app make a request back to itself

1,238 views
Skip to first unread message

Jason Broughton

unread,
Feb 6, 2015, 3:32:37 PM2/6/15
to expre...@googlegroups.com
Good Day
I have a weird requirement and I wouldn't mind picking some brains out there.

Our rest api has a route to create an Action. Actions have many different types.

Each action routes through a couple pieces of middleware to execute different logic.

There is an order of certain types of actions and an "open" action cannot happen until a 'receipt' action is created. 

We have one scenario where an 'open' action request can be sent before a 'receipt" action has been created and we want to modify the request to force a 'receipt' action to be saved before the open and preform a second request back to the api to route through the same endpoint to create the 'open' action.

My initial thought is accept the 'open' action and change the properties to make it a 'receipt' action, allow it to continue saving etc as a 'receipt', then post the same data back to the same url and this time when it checks for a receipt it finds the 'receipt' action and permits the 'open' action to be saved.

 I just want to send the request and not wait for the server response. Once sent, i would like that connection to close so there are no 'Error: socket hang up'.
Is there a better way to handle this? Im just using a request.post('same url').form(req.body);

thanks
JB

Jason Crawford

unread,
Feb 6, 2015, 4:19:55 PM2/6/15
to expre...@googlegroups.com
I don't totally understand your scenario, but if you want to have your server call itself, there's probably a better design/architecture you can find.

In general, you should have a layer of model objects that encapsulate all your business logic. If, in the course of serving a request, you need to invoke some logic, you should be able to do it by directly calling a method on an object, not by making another HTTP call to yourself.

Sorry, I know that's general, but I hope it helps.

-Jason

--
Blog: http://blog.jasoncrawford.org  |  Twitter: @jasoncrawford



--
You received this message because you are subscribed to the Google Groups "Express" group.
To unsubscribe from this group and stop receiving emails from it, send an email to express-js+...@googlegroups.com.
To post to this group, send email to expre...@googlegroups.com.
Visit this group at http://groups.google.com/group/express-js.
For more options, visit https://groups.google.com/d/optout.

Jason Broughton

unread,
Feb 11, 2015, 3:22:41 PM2/11/15
to expre...@googlegroups.com
Thanks Jason
Yea the more I thought about it, the more it felt like the worst course of action. I found a better way to create both objects at the same time. it was much simpler and would give me less of a risk of getting caught in some endless loop.

Cheers
Jason B 
Reply all
Reply to author
Forward
0 new messages