Need to reduce the timeout period for a route in expressjs

≡放荡痞女 提交于 2019-12-18 13:17:41

问题


Within expressjs is there a way I can set the timeout limit per route.

I have some routes that may take 30-45 seconds to process (A large amount of tasks)

And then other routes that if it takes longer than 5 seconds I want it to time out.

I guess I am asking is there a way to globally set the timeout limit on requests and is there a way to do it individually on routes.


回答1:


Use the built-in connect-timeout middleware:

http://www.senchalabs.org/connect/timeout.html

var connectTimeout = require('connect-timeout');

var timeout = connectTimeout({ time: 10000 });
var longTimeout = connectTimeout({ time: 45000 });

app.use(timeout); // you can set a global timeout value
app.get('/some/route', longTimeout, yourHandler); // or you can set per-route timeouts


来源:https://stackoverflow.com/questions/14408573/need-to-reduce-the-timeout-period-for-a-route-in-expressjs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!