gpt4 book ai didi

Node.js http 请求不工作

转载 作者:搜寻专家 更新时间:2023-10-31 22:53:13 24 4
gpt4 key购买 nike

node.js http 请求在我的机器上经常失败。我不知道发生了什么。我正在运行这个简单的死脚本:

var http = require("http");

var options = {
host: 'www.google.com',
port: 80,
path: '/upload',
method: 'GET'
};

var req = http.request(options, function(res) {
console.log('STATUS: ' + res.statusCode);
console.log('HEADERS: ' + JSON.stringify(res.headers));
res.setEncoding('utf8');
res.on('data', function (chunk) {
console.log('BODY: ' + chunk);
});
});
req.end();

请求永远挂断。我在 Node 0.4.5 上遇到了这个问题,我之前在 0.4.2 上遇到过。它有严重的影响,就像 npm 根本不起作用。我在 2010 Mac Book Pro 15"上运行 Node 并且有 os 10.6.7,就像 2 个同事连接在同一个 wifi 路由器上一样。任何人都知道发生了什么事?任何可能与任何其他应用程序发生冲突或在我的机器上运行的服务..问候

最佳答案

你的脚本对我有用,所以我不确定为什么它对你来说是挂起的……但是,像这样创建一个客户端对象怎么样……?也许会有帮助?

var http = require('http');
var google = http.createClient(80, 'www.google.com');
var request = google.request('GET', '/', {'host': 'www.google.com'});
request.on('response', function(response) {
console.log('STATUS: ' + response.statusCode);
console.log('HEADERS: ' + JSON.stringify(response.headers));
response.setEncoding('utf8');
response.on('data', function(chunk) {
console.log('BODY: ' + chunk);
});
});
request.end();

关于Node.js http 请求不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5648629/

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