HTTP GET request (Node) returns 501

2019-08-23 05:43发布

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" or "FOO" methods the console says:

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

1条回答
该账号已被封号
2楼-- · 2019-08-23 06:19

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

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

Your object should be:

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

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

const https = require('https');
查看更多
登录 后发表回答