I want to handle simultaneous user requests in my express server, and store data from them in a Mongo db. I have all the code ready to go, however, I'm struggling to find a way to assign a unique id to each user request so I can then save the data from each user to a Mongo db.
Here is a sample of my code below.
app.post("/page1", function(request,response) {
var ipAddress = request.connection.remoteAddress;
var name1 = request.body.name1;
MongooseModel.findOneAndUpdate({id: ipAddress},{name1:name1},
{upsert: false}, function() {});
response.redirect('/page2');
});
As you can see, I'm trying to differentiate each client request based on their IP address. The following code is then run directly after the code posted above.
app.post("/page2", function(request,response) {
var ipAddress = request.connection.remoteAddress;
var name2 = request.body.name2;
MongooseModel.findOneAndUpdate({id: ipAddress},{name2:name2},
{upsert: false}, function() {});
response.redirect('/page3');
});
Once again, I'm trying to differentiate each client request based on their IP address. Unfortunately, the code I have above only works if two (or more) clients don't share the same network connection. If they do, request.connection.remoteAddress returns the same value and my db gets confused.
Is there a clever solution to this problem? I know that most websites (such as amazon.com) have had to overcome this issue, as they receive millions of client requests and have to differentiate between all of them in order to store data in their databases.
question from:
https://stackoverflow.com/questions/65927794/handling-simultaneous-client-requests-in-node-express 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…