如何检测用户取消请求
我正在通过编写一个非常基本的http / webcaching代理尝试Node.js,并击中了我没有设法突破的东西。
假设我有一个非常基本的代理function(侦听请求,将其传送到外部服务器,等待响应,将其传回客户端),如何检测客户端(Web浏览器)何时取消请求? 当用户在他的浏览器上点击“Stop”/ Esc时,浏览器不会向我发送任何“请求”或信息,并且在“响应”连接结束时没有被调用时附加callback。
这是我的意思:
http.createServer (clientRequest, clientResponse) { var client = http.createClient (port, hostname); var request = client.request (method, url, headers); request.addListener ('response', function(response){ response.addListener ('data', function(chunk){ // forward data to clientResponse.. } response.addListener ('end', function(){ // end clientResponse.. } }); clientResponse.addListener('end', function(){ // this never gets called :( // I want it to terminate the request/response created just above } }
原来,我应该绑定到“closures”事件,而不是请求的“结束”事件。 这确实是有道理的。
我在这里发布这个可能遇到同样问题的其他人:
clientResponse.addListener('close', function(){ // this gets called when the user terminates his request }
你可能想要在这里回答超时吗?
- NPM安装在Docker容器内失败,但是在具有公司代理的主机上运行
- 根据URLpath创build代理
- 无论如何设置passportjs的代理设置?
- 为什么node.js不能用guerrillamail生成邮件?
- 在Node.js请求模块上设置{“agent”:false}
- 我如何使用https代理和node.js https / request客户端?
- proxyquire with require.exports = ClassConstructor
- 在一个端口上使用单个应用程序的节点应用程序使用代理服务器(如node-http-proxy)有什么意义?
- 服务器没有返回JSON从Express到React(代理)