简体   繁体   中英

Getting 204 using koa2 and koa-router for REST api - response body not being passed

I'm coming from Express and trying to learn Koa2 for a new project that I'm working on, but I'm struggling getting the most basic Get operation working for my app.

On the server side I have a route setup that is hitting an authorization server (Etrade), which returns an HTML link that the user will need to use to authorize the app.

I can use Postman to hit the route and see that I get the link back from Etrade through my console.log() call, but it is not coming back to Postman in the response body.

When I wired it up to the client app, I get a response status code of 204, which means my response body is empty if I'm understanding this correctly.

I need to figure out how to get the response body passed along as well as improve my understanding of Koa2.

I've currently setup my server.js as follows:

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}.`);

My router.js is setup as follows:

import Router from 'koa-router';
import etradeVerification from '../../server/api/etrade/verification';

const api = new Router({
    prefix: '/api'
});

etradeVerification(api);

export default api;

Finally the logic for the route, minus the key and secret stuff:

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);
}

Thanks for your help and guidance!

ctx.body = await et.getRequestToken(receiveVerificationUrl, failedToGetUrl);

This call to et.getRequestToken does not return anything. When await fires, it'll just get undefined. Normally I'd suggest using es6-promisify but it's also not a standard Node interface either (one callback, with an err and value arguments (Very disappointing!))

Perhaps create a function like the following to Promisify the function:

function getRequestToken(et){
    return new Promise(function(resolve, reject){
        et.getRequestToken(resolve, reject)
    })
}

Then you can ctx.body = await getRequestToken(et) .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM