John Smith
John Smith

Reputation: 347

Global context in node-red to store http req and res objects

We are exploring node-red as an option for rapidly creating small utilities needed within our team.

Typical pattern we have is :
1) Get search criteria from HTTP query parameters
2) Lookup records matching those in our couchdb
3) Call one of two APIs for each result in the array
4) Combine all results into one array and return response to original request

At the step 3 , there can be several msg payloads and the flow is somehow loosing original Http req and res objects. For the final step , we are storing them in global context and using again when all results are combined.

In case of multiple requests , is there a chance of the gloabal context being overwritten ? Is that shared across multiple requests ? Or it is safe in the above scenario ?

Upvotes: 1

Views: 788

Answers (1)

knolleary
knolleary

Reputation: 10117

In general, storing msg.req/res into context is an anti-pattern that should be avoided. As soon as you have to handle two requests in parallel, the second one will overwrite the first.

I would start by tracking down where exactly those properties are going missing in your flow. Nodes are meant to pass on all message properties. There are some edge cases where its acceptable not to, but they are very specific.

If you find a node dropping message properties it is likely a bug with that node that needs to be fixed.

Upvotes: 4

Related Questions