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

标签 javascript node.js http

我正在 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/

相关文章:

javascript - Linq.js 作用域

javascript - `module.exports = {__dirname}` 如何以及为何工作?

javascript - 指定/更改主干集合的端口以指向 couchdb 服务器

javascript - 我想在 fullCalendar.js 中更改日 View

javascript - `this` 的默认绑定(bind)与 Chrome 浏览器和 Node.js 不同

node.js - node fluent-ffmpeg流输出错误码1

http - 服务器每秒处理 70 个请求,每个请求的响应时间小于 50 毫秒

c# - 如何在 C# 中下载 zip 文件?

javascript - 使用jquery仅退出内部循环

node.js - 使用 JWT token 机制实现从所有设备功能注销的方法有哪些?