![](/img/trans.png)
[英]NodeJS - Express accept request if it is from specific URL or HOST
[英]Accept only first request from a bulk of requests in NodeJS and Express
我有一種情況,我將在不到5秒的時間內收到一百個請求,而我只想接受第一個請求並將其存儲到數據庫(mongodb)。 那么,對用戶的響應將是他們是否被接受。
您建議我使用哪種方法或庫? 我正在使用NodeJS和Express。 謝謝
1)將每個查詢的回調序列寫入數組
2)當第一個請求完成計算后-調用所有回調
// routes/index.js
var requests = [];
router.get('/:order', function(req, res, next) {
if (requests.length === 0) {
var f = function() {
res.json('first order complete');
}
requests.push( f );
// Process first request
setTimeout( function() {
while (requests.length > 0) {
requests.shift()();
}
requests = [];
}, 10000);
} else {
var f = function() {
res.json( req.params.order );
}
requests.push( f );
}
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.