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

5 Ways to Make HTTP Requests in Node.js

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

 来源: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” 的相关文章

深刻理解Linux进程间通信(IPC)

一个大型的应用系统,往往需要众多进程协作,进程(Linux进程概念见附1)间通信的重要性显而易见。本系列文章阐述了 Linux环境下的几种主要进程间通信手段,并针对每个通信手段关键技术环节给出详细实例。为达到阐明问题的目的,本文还对某些通信手段的内部实现机制进 行了分析。 序…

何小鹏:移动互联网不得不看的5个大坑

 这是UC创始人何小鹏在创新派主办PMx沙龙上的一次主题演讲,从产品经理的角度深度挖掘了移动互联网创新的那些坑,很干货,也很毁三观:...…

MS-Windows .bmp RLE4

In principle RLE4 coding is based on the same scheme as RLE8. The primary difference is the resolution of the colour index using 4 bit and a range o…

ATL.SubclassWindow分析

[B]ATL.SubclassWindow分析[/B] [template BOOL CWindowImplBaseT::SubclassWindow(HWND hWnd) { BOOL result; ATLASSUME(m_hWnd == NULL); ATLASSERT(::IsWi…

wince串口驱动相关

WindowsCE下的串口驱动程序能够处理所有I/O行为类似串口的设备,包 括基于16450、16550UART(通用异步收发芯片)的设备和一些采用DMA的设备,常见的有9针串口、红外I/O口、Modem等。 在%_WINCEROOT%\Public\Common\OAK\Drivers\Seria…

Android系统架构

android的系统架构和其操作系统一样,采用了分层的架构。从架构图看,android分为四个层,从高层到低层分别是应用程序层、应用程序框架层、系统运行库层和linux核心层。 1.应用程序 Android会同一系列核心应用程序包一起发布,该应用程序包包括email客户端,SMS短消息程序,…

发表评论

访客

看不清,换一张

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