知了博客

集天地之精华,吸日月之灵气

« 我国全过程人民民主是民主含量高、民主成色足、深受中国人民欢迎的民主江苏省委省政府调查组关于“丰县生育八孩女子”事件调查处理情况的通报 »

5 Ways to Make HTTP Requests in Node.js

 来源: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); });
  • 相关文章:

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

日历

最新评论及回复

最近发表

Powered By Z-Blog 1.8 Arwen Build 90619 Code detection by Codefense  theme by BokeZhuti

Copyright know blog. Some Rights Reserved.站长(msn):webmaster#webgou.info(#换成@) 粤ICP备09183716号