如何处理nodejs中对API的并发访问

我目前正面临一个问题。 纠正我,如果我错了。 在我的项目中,我有一个NodeJS服务器有mongoDB数据库和一个android应用程序连接到它。

有4个Android设备连接到它,当服务器发布列表,它通过Socket.IO发送一个发送呼叫到所有的Android设备。 android收到发出呼叫的那一刻,它调用API来获取发布的列表。 这里请求调用(4个请求)同时到达服务器。

在这里,设备出现了意外的分配。 意外的是,有一些参数是基于哪些设备进行分配的。 当只有一个设备在线时,这个场景完美地工作,但是当并发请求从多个设备到达时,有时会出现相关的问题。

是否可以一次处理一个并发请求?

讨论/build议是受欢迎的。 执行stream程在这里

  flowController.on('START', function () { // Uploading and storing the binary data as excel file }); flowController.on('1', function () { // Validation to check if file contain required sheet with name and all and convert the file into JSON format }); flowController.on('2', function () { // Replacing the keys from the JSON to match the required keys }); flowController.on('3', function () { // Massive validation of records & seperating the records as per the activity }); // Activity 1 flowController.on('4', function () { // Insert records of Activity 1 }); // Activity 2 flowController.on('5', function () { // Insert records of Activity 2 }); // Activity 3 flowController.on('6', function () { // Insert records of Activity 3 }); // Activity 3 flowController.on('END', function () { // End }); 

如果你想一次处理一个请求,你可以实现一个队列。

 const queue = []; const processingQueue = false; socketServer.on('connection', (connection) => { connection.on('SOME_EVENT', (data, callback) => { queue.push({data: data, callback: callback}); }); }); function processQueue() { async.whilst(() => queue.length, (callback) => { var request = queue.shift() // process request callback(); }, (err, n) => { setTimeout(processQueue, 1); }); } processQueue(); 

但是,如果你分享一些代码,这个问题可能会有更好的解决scheme。