当前位置:首页 > 编程开发

5 Ways to Make HTTP Requests in Node.js

webgou5年前 (2021-12-04)编程开发209

 来源: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); });

扫描二维码推送至手机访问。

版权声明:本文由知了博客发布,如需转载请注明出处。

本文链接:https://www.webgou.info/?id=665

分享给朋友:

“5 Ways to Make HTTP Requests in Node.js” 的相关文章

Git Release Notes (Git-1.7.7-preview20111012)

Git Release Notes (Git-1.7.7-preview20111012)…

Android根文件系统相关应用介绍

我们曾经在一篇文章中为大家详细介绍过Android文件系统的一些基本概念,相信大家已经可以充分了解这一知识点了。在这里大家将会继续对Android根文件系统的基本概念进行一个初步的掌握。经过这几天在网上搜索关于Android根文件系统的相关资料,发现已经有很多高手 分析了Android的文件系统,这…

通往WinDbg的捷径(二)

 保存 dumps 在 我们调试不容易重现的问题时,可能想把应用程序状态的快照(内存内容,打开名柄的列表,等等)保存起来,以便日后分析。例如,当我怀疑当前的状态可能包含 我试图解决的问…

Android初体验一

今天,在windows下搭建Android开发环境,步聚比较简洁。昨晚在自已电脑上,由于ADSL网速原因,安装ADT时卡住了,今天在公司很快就搞定了:1.安装android sdk 2.下载安装eclipse ADT 3.调试其中的例子,运行AVD查看效果...…

跟alman病毒战斗

今天发现中了Virus.Win32.Alman病毒病毒,与之战斗了一个下午,将过程大概描述一下,希望能给大家提供一个参考。     卡 巴斯基、symantec、nod32可以识别,斯基经过反复查杀可以清理掉,不过exe就完全损失了,需要重新安装。nod可以隔离,但是清…

发表评论

访客

看不清,换一张

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