标准库中默认的HTTP模块
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 recieved. 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); });
需要以区块为单位接收响应数据
HTTP和HTTPS协议分属两个模块

Request
npm install request
npm install request@2.81.0
const request = require('request'); request('https://api.nasa.gov/planetary/apod?api_key=DEMO_KEY', { json: true }, (err, res, body) => { if (err) { return console.log(err); } console.log(body.url); console.log(body.explanation); });
或者通过
) { console.log(body) ) { } })

node.js发http请求的更多相关文章

  1. Node.js GET/POST请求

    在很多场景中,我们的服务器都需要跟用户的浏览器打交道,如表单提交. 表单提交到服务器一般都使用GET/POST请求. 我将为大家介绍 Node.js GET/POST请求. 获取GET请求内容 由于G ...

  2. node.js 发送http 请求

    自己研究了一下 node.js 的 http模块  下面为想服务器发送请求的代码 ,通过学习了解http 请求的过程 ,node.js 对http请求的原始封装比较低,以前php 可以用$_GET , ...

  3. body-parser Node.js(Express) HTTP请求体解析中间件

    body-parser Node.js(Express) HTTP请求体解析中间件 2016年06月08日     781     声明 在HTTP请求中,POST.PUT和PATCH三种请求方法中包 ...

  4. 关于Node.js的httpClieint请求报错ECONNRESET的原因和解决措施

    背景说明 最近在工作项目中有下面一个场景: 使用Node.js的express框架实现了一个文件系统服务器端,其中有个API用于客户端上传文件.客户端使用Node.js的HttpClient来调用服务 ...

  5. node.js搭建代理服务器请求数据

    1.引入node.js中的模块 var http = require("http"); var url = require("url"); var qs = r ...

  6. formidable处理node.js的post请求

    前言 我们都知道在node.js中,我们最常用的请求方式是get和post.其中get请求和URL相关,通过解析URL我们可以直接获取到请求的参数.但是post请求不同,post请求是包含在请求体中, ...

  7. 35.Node.js GET/POST请求

    转自:http://www.runoob.com/nodejs/nodejs-module-system.html 在很多场景中,我们的服务器都需要跟用户的浏览器打交道,如表单提交. 表单提交到服务器 ...

  8. node.js发邮件

    在node上使用第三方类库(nodemailer)发邮件是一件很esay的事情:) app.js   以QQ邮箱为例 var nodemailer = require('nodemailer'); v ...

  9. node.js处理post请求

    1.html 2.app.js var http = require('http') var qs = require('querystring') /** * 路由控制的功能 * @param pa ...

随机推荐

  1. Unity for Windows: II – Publishing Unity games to Windows Store

    原地址:http://digitalerr0r.wordpress.com/2013/08/27/unity-for-windows-ii-publishing-to-windows-8/ Windo ...

  2. MySQL 连接方式

    MySQL 连接方式 1:TCP/IP 套接字方式 这种方式会在TCP/IP 连接上建立一个基于网络的连接请求,一般是client连接跑在Server上的MySQL实例,2台机器通过一个TCP/IP ...

  3. TCP 中出现RST的情况

    http://www.360doc.com/content/13/0702/10/1073512_297069771.shtml 原 几种TCP连接中出现RST的情况 发表于1年前(2013-05-0 ...

  4. 树莓派/RaspberryPi 内核源码下载

    树莓派的源码有两种下载方式:压缩包下载和git clone指令下载. 1.压缩包下载 选择对应分支,点击Github界面的 下载按钮即可,如下图: 测试发现,同样的分支,用压缩包方式下载后编译会出错, ...

  5. C#:TextBox控件操作类

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.W ...

  6. for循环和增强版的for循环

    增强的for循环. 缺点:   对于数组.不能方便的訪问下标值.   对于集合,与使用Interator相比.不能方便的删除集合中的内容(在内部也是调用Interator). 除了简单遍历并读取当中的 ...

  7. Jquery实现无刷新DropDownList联动

    <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> &l ...

  8. XML文件标签名一致,而属性值不同,如何遍历取值写法 摘录

    <EssentialFunctions>      <Qualification description="We Offer" source="AdDe ...

  9. Spring Cloud(三):服务提供与调用

    上一篇文章我们介绍了eureka服务注册中心的搭建,这篇文章介绍一下如何使用eureka服务注册中心,搭建一个简单的服务端注册服务,客户端去调用服务使用的案例. 案例中有三个角色:服务注册中心.服务提 ...

  10. 严重: Dispatcher initialization failed java.lang.RuntimeException: java.lang.reflect.Invoc

    错误提示:严重: Dispatcher initialization failed java.lang.RuntimeException: java.lang.reflect.InvocationTa ...