gpt4 book ai didi

javascript - 异步函数抛出错误: socket hang up

转载 作者:行者123 更新时间:2023-12-02 14:55:21 24 4
gpt4 key购买 nike

我正在 http 请求函数内调用异步函数。被调用的异步函数执行另一个 http 请求,但该 http 请求抛出错误 错误:套接字挂起

代码

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

var url_1 = "en.wikipedia.org";
var url_2 = "www.twitter.com";

var options = { host: url_1, port: 80, path: '', method: 'POST'};
var api_options = { host:url_2, port: 80,path: '',method: 'POST' };


function async(arg, callback) { //async function declaration
console.log("received argument "+ arg);
http.request(options, function(res){ //http request to url_2
console.log("also the second http request successful");//this log not getting printed, insted http.request function in the previous line throws Error : socket hang up
return callback(arg);
});
}


http.request(options, function(res) { //http request url_1
console.log("first http request successful");
async("1",function(return_value){ //calling asynchronous function
console.log("count : "+ return_value + " returns callback");
});
}).end();

在上面的代码中,我首先向url_1发出http请求,请求后我正在调用async函数,该函数尝试向发出http请求>url_2,但是 async 函数内的 url_2 http 请求只会返回我上面提到的错误。

结果

first http request successful                                                                                                                      
received argument 1
events.js:141
throw er; // Unhandled 'error' event
^

Error: socket hang up
at createHangUpError (_http_client.js:203:15)
at Socket.socketOnEnd (_http_client.js:288:23)
at emitNone (events.js:72:20)
at Socket.emit (events.js:166:7)
at endReadableNT (_stream_readable.js:905:12)
at doNTCallback2 (node.js:441:9)
at process._tickCallback (node.js:355:17)


Process exited with code: 1

我是 Node.js 新手,所以如果这是一个愚蠢的问题,请原谅

最佳答案

对于初学者:“www.twitter.com”是301(永久移动)

但是,当您使用 Node 时,您就会拥有很棒的node_modules。为什么不使用它们呢?

这是 got 和 Promise 的样子:

var request = require('got');

var url_1 = "en.wikipedia.org";
var url_2 = "google.com";

var options = { host: url_1, port: 80, path: '', method: 'POST'};
var api_options = { host:url_2, port: 80,path: '',method: 'POST' };

request(options).then(function (res) {
console.log('call1 ok');
return request(api_options);
}).then(function (res) {
console.log('call2 ok');
}).catch(function (err) {
console.error(err);
});

安装got ,只需使用:

npm install got
<小时/>

选项2,如果你真的想使用node http模块,这里有一个丑陋的东西:

var http = require("http");  

var url_1 = "en.wikipedia.org";
var url_2 = "google.com";

var options = { host: url_1, port: 80, path: '', method: 'POST'};
var api_options = { host:url_2, port: 80,path: '',method: 'POST' };

var req1, req2;

req1 = http.request(options, function (res) {
res.on('data', function () {
console.log('recieved data from req1')
})
res.on('end', function () {
console.log('call1 ok')
req2 = http.request(api_options, function (res2) {
res2.on('end', function (body) {
console.log('call2 ok')
});
res2.on('data', function () {
console.log('recieved data from req2')
});
});
req2.end();
});
});
req1.end();

如果你不.end()你的http.request(),它会抛出一个套接字挂起错误。请参阅Node documentation

关于javascript - 异步函数抛出错误: socket hang up,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35858456/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com