5 Ways to Make HTTP Requests in Node.js

游客

标题:5 Ways to Make HTTP Requests in Node.js
正文:
来源：https://www.twilio.com/blog/2017/08/http-requests-in-node-js.htmlHTTP &ndash; the Standard LibraryFirst 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&rsquo;t very user friendly compared to other solutions.The following code will send a GET request to NASA&rsquo;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) =&gt; { let data = ''; // A chunk of data has been received. resp.on('data', (chunk) =&gt; { data += chunk; }); // The whole response has been received. Print out the result. resp.on('end', () =&gt; { console.log(JSON.parse(data).explanation); }); }).on(&quot;error&quot;, (err) =&gt; { console.log(&quot;Error: &quot; + err.message);
[<<][[1]][2][>>]

查看评论(0)
发表评论

首页