【问题标题】:node.js: how to stop an already started http requestnode.js:如何停止已经启动的 http 请求
【发布时间】:2016-03-28 10:37:24
【问题描述】:

我们使用request 通过 node.js 发出 http 请求。 我们希望用户有机会在他决定时中止请求。这意味着我们必须从请求函数之外触发 abort()。也许我们可以从函数内部检查一个外部变量。 (我们已经尝试在请求开始后将超时设置为零,但这不起作用。) 也许我们将变量请求设置为空。你知道更好的方法吗?

这里是显示我们正在讨论的请求类型的示例代码:

app.js:

var http = require('http');
var request = require("request");

http.createServer().listen(1337, "127.0.0.1");

request({uri: 'http://stackoverflow.com'  }, function (error, response, body) {
    console.log('url requested ') ;
    if (!error){
        console.log(body);
    }
    else
    {
        console.log(error);
    }
});

【问题讨论】:

    标签: javascript node.js


    【解决方案1】:

    请求函数返回一个请求对象。只需致电abort() 即可?

    var r = request({uri: 'http://stackoverflow.com'  }, function (error, response, body) {
        console.log('url requested ') ;
        if (!error){
            console.log(body);
        }
        else
        {
            console.log(error);
        }
    });
    
    r.abort();
    

    【讨论】:

    • 能快速回答node.js问题的人不多。你就是其中之一。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-08-13
    • 2011-09-28
    • 2016-12-11
    • 1970-01-01
    • 2021-11-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多