简体   繁体   中英

HTTP GET request (Node) returns 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" or "FOO" methods the console says:

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

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');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM