cURL equivalent in Node.js?

后端 未结 18 589
误落风尘
误落风尘 2020-11-29 16:43

I\'m looking to use information from an HTTP request using Node.js (i.e. call a remote web service and echo the response to the client).

In PHP I would have used cUR

18条回答
  •  野趣味
    野趣味 (楼主)
    2020-11-29 17:02

    Use request npm module and after call

    var request = require('request');
    request('http://www.google.com', function (error, response, body) {
      console.log('error:', error); // Print the error if one occurred
      console.log('statusCode:', response && response.statusCode); // Print the response status code if a response was received
      console.log('body:', body); // Print the HTML for the Google homepage.
    });
    

    For best practice also use some winston logger module or else simple console.log and then run your application like

    npm start output.txt 
    

    Result of above command will generate one txt file on root with all data which you have printed in console.log

提交回复
热议问题