基本的な操作は私のアプリのために働く。私はExpressから来て、私が取り組んでいる新しいプロジェクトのためにKoa2を学ぼうとしていますが、私が最も苦労しているのは、Koa2とKoa-Rootを使ってREST APIレスポンスボディが渡されないようにすることです。
サーバー側では、許可サーバー(Etrade)に接続するルート設定があります。このルート設定では、ユーザーがアプリケーションを承認するために使用するHTMLリンクが返されます。
私は郵便配達員を使って経路を突き止め、私のconsole.log()
コールを通じてEtradeからのリンクを返すことができますが、返信の本体ではPostmanに戻っていません。
クライアントアプリケーションに接続すると、応答ステータスコード204が返されます。これは、正しく理解していれば、応答本体が空であることを意味します。
私はKoa2の理解を向上させるだけでなく、レスポンスボディを渡す方法を理解する必要があります。
私は次のように現在のセットアップ私のserver.js
をしました:
import Koa from 'koa';
import convert from 'koa-convert';
import proxy from 'koa-proxy';
import logger from 'koa-logger';
import body from 'koa-better-body';
import api from '../config/router/router';
import historyApiFallback from 'koa-connect-history-api-fallback';
import config from '../config/base.config';
const port = config.server_port;
const host = config.server_host;
const app = new Koa();
app.use(logger());
app.use(body());
app.use(api.routes());
app.use(api.allowedMethods());
// enable koa-proxyy if it has been enabled in the config
if (config.proxy && config.proxy.enabled) {
app.use(convert(proxy(config.proxy.options)));
}
app.use(convert(historyApiFallback({
verbose : false
})));
server.listen(port);
console.log(`Server is now running at http://${host}:${port}.`);
次のように私のrouter.js
を設置している:
import Router from 'koa-router';
import etradeVerification from '../../server/api/etrade/verification';
const api = new Router({
prefix: '/api'
});
etradeVerification(api);
export default api;
最後にルートのロジック、マイナス鍵と秘密のもの:
をimport Etrade from 'node-etrade-api';
const myKey = '';
const mySecret = '';
const configuration = {
useSandbox : true,
key : myKey,
secret : mySecret
};
const et = new Etrade(configuration);
export default function(router) {
router.get('/etrade', getEtradeUrl);
}
async function getEtradeUrl(ctx, next) {
// Isn't this how I send the response back to the client?
// This isn't coming through as a response body when using Postman or the client app
ctx.body = await et.getRequestToken(receiveVerificationUrl, failedToGetUrl);
}
function receiveVerificationUrl(url) {
console.log(url); // This works and displays the response from etrade
return url
}
function failedToGetUrl(error) {
console.log('Error encountered while attempting to retrieve a request token: ', error);
}
ご協力いただきありがとうございます。
こんにちは、私は体にも問題があります。私はWindows10を使用しています。私はこの質問に引用された私の問題に対する解決策を見つけることができませんでした。http://stackoverflow.com/questions/38856815/koa2-request-body-is-emptyあなたのオペレーティングシステムは何ですか? –