Can Express.js has race status at the post

Node noob question here, I'm sure.

I have the code below in a simple express JS application

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 not viewing the Node request workflow ...

Is a race condition possible when two message requests are processed simultaneously /match, so that both records try to call randomPin.generate()?

If this is the best way to avoid this?

+3
source share
2 answers

If there are two POST requests /match, the second request will wait for the completion of the first request. However, if your mail handler updates any global variables or object (for example, cache), this change will be visible to other requests.

randomPin.generate() , Node.js , .

: Event Loop Node.js

+5

, , , :

Node . , , , . , 1 /match pin=pin, 2 /match pin=pin . node , ... , , node . , . randomPin.generate(). , currentPin .

+3

All Articles