HTTP GET请求(节点)返回501 [英] HTTP GET request (Node) returns 501

查看:1808
本文介绍了HTTP GET请求(节点)返回501的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Node上测试虚假的HTTP请求。但是我在定义GET,POST方法或FOO的头文件上得到了相同的结果(501)。我不明白输出。有人可以给我一个暗示吗?代码:

I'm testing fake HTTP requests on Node. But I get the same result (501) on the headers defining GET, POST methods or "FOO". I don't understand the output. Can someone give me a hint? The code:

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方法:

"GET" or "FOO" methods the console says:

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


推荐答案

选项对象没有 uri 键,你应该使用 hostname

The options object has no uri key, you should use hostname.

另外,不要在主机内指定协议,使用密钥 protocol

Also, do not specify the protocol inside the host, use the key protocol.

您的对象应为:

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

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

Remember that to use https you need to include the right module:

const https = require('https');

这篇关于HTTP GET请求(节点)返回501的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆