本文转载自:众成翻译
译者:网络埋伏纪事
连接:http://www.zcfy.cc/article/1758
原文:https://blog.risingstack.com/node-hero-node-js-request-module-tutorial/html
在以下教程中,将学习 HTTP 协议的基础知识,以及如何使用 Node.js 的 request 模块从外部源获取资源。node
HTTP 表明超文本传输协议(Hypertext Transfer Protocol)。HTTP 做为在客户机-服务器计算模型中的请求-响应协议。express
在深刻与其它 API 的通信以前,咱们来回顾一下在此过程当中会遇到的 HTTP 状态码。状态码描述了请求的结果,它对错误处理是必不可少的。npm
1xx - 响应还没完成(Informational)json
2xx - 成功:这些状态码表示请求被接收并正确处理了。最多见的成功响应码是 200 OK
、201 Created
和 204 No Content
。api
3xx - 重定向: 这组状态码表示要完成请求,客户端必需要作一些额外的操做。最多见的重定向响应码是 301 Moved Permanently
、304 Not Modified
。promise
4xx - 客户端错误: 这类状态码被用在客户端发送的请求出现了某种错误时。服务器响应一般会包含了对错误的解释。最多见的客户端错误码是 400 Bad Request
、401 Unauthorized
、403 Forbidden
、404 Not Found
、409 Conflict
。服务器
5xx - 服务器错误: 当服务器因为某些错误不能完成有效的请求时,就发送这些状态码。缘由多是代码中的一个 bug,或者一些临时或者永久性的无能。最多见的服务器错误状态码是 500 Internal Server Error
、503 Service Unavailable
。若是你想学习更多有关 HTTP 状态码的知识,能够在这里找到详细的解释。网络
在 Node 中,链接到外部 API 是很简单的。只须要 require 核心 HTTP 模块,而后开始发送请求便可。app
固然,有更好的方式来调用外部端点。在 NPM 上能够找到不少让这个过程更容易的模块。例如,两个最流行的模块是 request 和 superagent。
这两个模块都有错误优先的回调接口,这就会致使一些问题(我打赌你已经据说过回调地狱),可是幸运的是咱们能够访问封装了 Promise 的版本。
request-promise 模块 的使用很简单。从 NPM 安装它以后,只须要 require 它:
const request = require('request-promise')
发送一个 GET 请求也那么简单:
const options = { method: 'GET', uri: 'https://risingstack.com' } request(options) .then(function (response) { // Request was successful, use the response object at will }) .catch(function (err) { // Something bad happened, handle the error })
若是调用一个 JSON API,你可能会想让 request-promise 自动解析响应。此时,只须要将其添加到 request 选项便可:
json: true
POST 请求的工做方式也相似:
const options = { method: 'POST', uri: 'https://risingstack.com/login', body: { foo: 'bar' }, json: true // JSON stringifies the body automatically } request(options) .then(function (response) { // Handle the response }) .catch(function (err) { // Deal with the error })
要添加查询字符串参数,只须要将 qs
属性添加到 options 对象:
const options = { method: 'GET', uri: 'https://risingstack.com', qs: { limit: 10, skip: 20, sort: 'asc' } }
这会让请求 URL 变为:https://risingstack.com?limit=10&skip=20&sort=asc
。
还能够用添加查询参数一样的方式来定义全部请求头:
const options = { method: 'GET', uri: 'https://risingstack.com', headers: { 'User-Agent': 'Request-Promise', 'Authorization': 'Basic QWxhZGRpbjpPcGVuU2VzYW1l' } }
错误处理是对外部 API 发起请求的重要部分,由于不能确保会发生什么。除了客户端错误之外,服务器会响应一个错误,或者就发送一个错误或者不一致格式的数据。在试着处理响应时,要记住这些。此外,对每次请求都使用 catch
也是一个避免外部服务让服务器崩溃的好办法。
既然已经学过如何建立一个 Node.js HTTP 服务器,如何渲染 HTTP 页面,如何从外部 API 获取数据,那么如今就到了将它们放在一块儿的时候了!
在本例中,咱们将建立一个小型 Express 应用程序,该程序基于城市名渲染当前的天气条件。
要获取 AccuWeather API key,请访问 Accuweather 开发者网站。
const express = require('express') const rp = require('request-promise') const exphbs = require('express-handlebars') const app = express() app.engine('.hbs', exphbs({ defaultLayout: 'main', extname: '.hbs', layoutsDir: path.join(__dirname, 'views/layouts') })) app.set('view engine', '.hbs') app.set('views', path.join(__dirname, 'views')) app.get('/:city', (req, res) => { rp({ uri: 'http://apidev.accuweather.com/locations/v1/search', qs: { q: req.params.city, apiKey: 'api-key' // Use your accuweather API key here }, json: true }) .then((data) => { res.render('index', data) }) .catch((err) => { console.log(err) res.render('error') }) }) app.listen(3000)
上面的示例作以下事情:
建立一个 Express 服务器
设置 handlebars 结构 - 对于 .hbs
文件,请参考 Node.js HTTP 教程
发送请求给外部 API
若是一切顺利,就渲染页面
不然,就显示错误页并记录错误
下章将学习如何正确组织 Node.js 项目。