在 Node.js 中使用超级代理 ajax 库
Use of superagent ajax library in Node.js
我是 javascript 构建堆栈的新手。
抱歉,如果我的问题标题可能与其他问题相似,但我还没有找到可以回答我问题的标题。
目前正在学习 React + Redux 并遵循本教程:https://thinkster.io/tutorials/react-redux-ajax-middleware
这是有问题的代码:
'use strict';
import superagentPromise from 'superagent-promise';
import _superagent from 'superagent';
const superagent = superagentPromise(_superagent, global.Promise);
const API_ROOT = 'https://conduit.productionready.io/api';
const responseBody = res => res.body;
const requests = {
get: url =>
superagent.get(`${API_ROOT}${url}`).then(responseBody)
};
const Articles = {
all: page =>
requests.get(`/articles?limit=10`)
};
export default {
Articles
};
真题:
为什么要用superagent-promise
? Doesn't superagent这样说:
ES6 promises are supported. Instead of .end()
you can call .then()
这个global.Promise
从哪里来的?我没有写过 global.Promise = require('some-promise-library')
之类的东西,AFAIK Node 也没有默认值 global.Promise
。我在这里错过了什么?
如果你想使用支持 promise 的超级代理,你需要那个 superagent-promise 包装器模块。就这些了
Node.js 原生支持 Promise,并且可以在全球范围内访问。
每个全局对象都可以在不加载它们的情况下使用,如进程、setTimeout 等。如果您使用本机 Promise,您可以只使用 Promise 而没有全局。
Object.prototype.hasOwnProperty.call(global,'Promise') 这个returns true
看起来您正在服务器上使用它。假设您没有使用像 0.10 这样的超旧版本的 Node,那么它有 Promise 支持。
superagent-promise 曾经是必需的 - 它不再是最新版本的 superagent。只需单独使用 superagent 并使用 built-in promise 支持。
如果您需要在客户端使用 superagent,那么任何 promise polyfill 都可以支持旧版浏览器(例如 babel-polyfill)。
我是 javascript 构建堆栈的新手。 抱歉,如果我的问题标题可能与其他问题相似,但我还没有找到可以回答我问题的标题。
目前正在学习 React + Redux 并遵循本教程:https://thinkster.io/tutorials/react-redux-ajax-middleware
这是有问题的代码:
'use strict';
import superagentPromise from 'superagent-promise';
import _superagent from 'superagent';
const superagent = superagentPromise(_superagent, global.Promise);
const API_ROOT = 'https://conduit.productionready.io/api';
const responseBody = res => res.body;
const requests = {
get: url =>
superagent.get(`${API_ROOT}${url}`).then(responseBody)
};
const Articles = {
all: page =>
requests.get(`/articles?limit=10`)
};
export default {
Articles
};
真题:
为什么要用
superagent-promise
? Doesn't superagent这样说:ES6 promises are supported. Instead of
.end()
you can call.then()
这个
global.Promise
从哪里来的?我没有写过global.Promise = require('some-promise-library')
之类的东西,AFAIK Node 也没有默认值global.Promise
。我在这里错过了什么?
如果你想使用支持 promise 的超级代理,你需要那个 superagent-promise 包装器模块。就这些了
Node.js 原生支持 Promise,并且可以在全球范围内访问。
每个全局对象都可以在不加载它们的情况下使用,如进程、setTimeout 等。如果您使用本机 Promise,您可以只使用 Promise 而没有全局。Object.prototype.hasOwnProperty.call(global,'Promise') 这个returns true
看起来您正在服务器上使用它。假设您没有使用像 0.10 这样的超旧版本的 Node,那么它有 Promise 支持。
superagent-promise 曾经是必需的 - 它不再是最新版本的 superagent。只需单独使用 superagent 并使用 built-in promise 支持。
如果您需要在客户端使用 superagent,那么任何 promise polyfill 都可以支持旧版浏览器(例如 babel-polyfill)。