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

5 Ways to Make HTTP Requests in Node.js

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

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

ARM寄存器简介

ARM寄存器介绍 ARM处理器共有37个寄存器。其中包括:31个通用寄存器,包括程序计数器(PC)在内。这些寄存器都是32位寄存器。以及6个32位状态寄存器。但目前只使用了其中12位。ARM处理器共有7种不同的处理器模式,在每一种处理器模式中有一组相应的寄存器组。任意时刻(也就是任意的处理器模式下)…

SOAP协议规范

SOAP协议规范1. 简介SOAP 以XML形式提供了一个简单、轻量的用于在分散或分布环境中交换结构化和类型信息的机制。SOAP本身并没有定义任何应用程序语义,如编程模型或特定语义的实现;实际上它通过提供一个有标准组件的包模型和在模块中编码数据的机制,定义了一个简单的表示应用程序语义的机制。这使SO…

如何诊断Windows CE的应用程序崩溃(Remote Process Explorer使用)

如何诊断Windows CE的应用程序崩溃(Remote Process Explorer使用)…

线程同步

效率      you should try to use nonshared data first and then use volatile reads, volatile writes, interlocked APIs, SRWL…

MPEG2-TS流文件的简介

随着从HDTV录制的高清节目在网上的流传,烧友们现在对TS这个名词大概已经不陌生了,但随之而来就是如何播放、如何添加字幕等等的一系列问题,本文将重点介绍一下这方面的应用操作。 先来简要介绍一下什么是MPEG2-TS吧。MPEG2格式大家都通过对DVD的接触而多多少少了解了一些,DVD节目中的M…

CMMB系统

 CMMB采用卫星和地面网络相结合的“天地一体、星网结合、统一标准、全国漫游”方式,实现全国…

发表评论

访客

看不清,换一张

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