gpt4 book ai didi

javascript - HTTP GET 请求( Node )返回 501

转载 作者:行者123 更新时间:2023-12-01 03:39:35 25 4
gpt4 key购买 nike

我正在 Node.js 上测试虚假的 HTTP 请求。但我在定义 GET、POST 方法或“FOO”的 header 上得到相同的结果 (501)。我不明白输出。有人可以给我提示吗?代码:

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


var options = {
method: "FOO" //or GET
, uri: 'https://www.google.com'


};

var callback = function(response){
var exportJson= JSON.stringify(response.headers);
var arrayData =[];
response.on('data', function(data) {
arrayData += data;

});

response.on('end', function() {
console.log('THE DATA IS ' + arrayData);


});
fs.appendFile("input.txt", exportJson, function(err) {
if(err) {
return console.log(err);
}
});

}



var req = http.request(options, callback);


function test(){

for (var prop in options.method) {
//console.log(`options.method${prop} = ${options.method[prop]}`);
//console.log(req);
req;
}

}

test();
req.end();

控制台显示“GET”或“FOO”方法:

<h2>HTTP ERROR 500.19 - Internal Server Error</h2>

最佳答案

options object没有 uri 键,您应该使用 hostname

另外,不要指定主机内部的协议(protocol),使用 key protocol

你的对象应该是:

const options = {
hostname: 'www.google.com',
protocol: 'https:',
}

请记住,要使用 https,您需要包含正确的模块:

const https = require('https');

关于javascript - HTTP GET 请求( Node )返回 501,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44000021/

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