来源:https://www.twilio.com/blog/2017/08/http-requests-in-node-js.html
HTTP – the Standard Library
First on our hit parade is the default HTTP
module in the standard library. With this module, you can just plug and go without having to install external dependencies. The downside is that it isn’t very user friendly compared to other solutions.
The following code will send a GET
request to NASA’s API and print out the URL for the astronomy picture of the day as well as an explanation:
const https = require('https'); https.get('https://api.nasa.gov/planetary/apod?api_key=DEMO_KEY', (resp) => { let data = ''; // A chunk of data has been received. resp.on('data', (chunk) => { data += chunk; }); // The whole response has been received. Print out the result. resp.on('end', () => { console.log(JSON.parse(data).explanation); }); }).on("error", (err) => { console.log("Error: " + err.message); });