【发布时间】:2018-07-31 07:04:35
【问题描述】:
我正在编写一个 API,它为任何给定页面返回一个重定向数组:
router.post('/trace', function(req,res){
if(!req.body.link)
return res.status(405).send(""); //error: no link provided!
console.log("\tapi/trace()", req.body.link);
var redirects = [];
function exit(goodbye){
if(goodbye)
console.log(goodbye);
res.status(200).send(JSON.stringify(redirects)); //end
}
function getRedirect(link){
request({ url: link, followRedirect: false }, function (err, response, body) {
if(err)
exit(err);
else if(response.headers.location){
redirects.push(response.headers.location);
getRedirect(response.headers.location);
}
else
exit(); //all done!
});
}
getRedirect(req.body.link);
});
这里是对应的浏览器请求:
$.post('/api/trace', { link: l }, cb);
一个页面会很快发出大约 1000 个 post 请求,然后等待很长时间才能得到每个请求。
问题是对第 n 个请求的响应非常很慢。单个请求大约需要半秒钟,但最好我不能告诉快速服务器正在按顺序处理每个链接。我希望服务器发出所有请求并在 it 收到响应时做出响应。
我是否正确假设 express POST 路由器按顺序运行进程?我如何让它爆炸所有请求并在收到响应时传递响应?
【问题讨论】:
-
您的问题到底是什么?从帖子中看不清楚
-
@djfdev 抱歉!我的问题是为什么这么慢/是在“开箱即用”的快速服务器上发布异步进程吗?
标签: node.js rest express post python-requests