Can Express.js have a race condition on post

express, node.js, race-condition

Solution

If there are two POST `/match` requests, second request will wait until the first request is completed. However, if your post handler updates any global variables or object (e.g. cache), that change will be visible to other requests.

In your case `randomPin.generate()` will not have a race condition problem as there is no such thing as simultaneous execution in Node.js.

You can read more on that here: Single threaded and Event Loop in Node.js

Problem

Node noob question here I'm sure. I have the below code in a simple express JS app ``` var randomPin = require('./api/randomPin'); var currentPin = "pin"; app.post('/match', function(req, res) { if (req.body.pin && req.body.pin == currentPin) { //it should only be possible for one person to get here //and receive this hurrah currentPin = randomPin.generate(); res.send({hurrah:true}); } res.send({hurrah:false}); }); ``` I'm still don't grok the workflow of a Node request... Is it possible for a race condition to arise where two post requests to `/match` are being processed at the same time such that both posts are trying to call `randomPin.generate()`? If so is there a 'best way' of avoiding this?

Original source

Related problems