0
votes

I am trying to configure proxy middleware koa-proxy. It works fine for all GET requests but POST requests with body don't reach the actual API.

let koa = require('koa');
let staticServe = require('koa-static');
let bodyParser = require('koa-bodyparser');
let config = require('./config');
let proxy = require('koa-proxy')({ host: 'config.API_URL' });

let app = koa();

app.use(staticServe(config.staticPath));
app.use(bodyParser());

app.use(proxy);

app.listen(config.port);
console.log(`Listening on port ${config.port}`);

How should I configure it to be able to make all requests from the app to the localhost which will be then directed to the API URL with the body and processed properly?

2
Did you try removing app.use(bodyParser())? koa-proxy internally uses pipe, so you don't need to parse the body unless you want log or use it.zeronone

2 Answers

1
votes

Well. You should confirm the data structure of your origin data and the data proxy by the koa-proxy.

In my case, the origin data content-type is 'x-www-urlencoded' but proxy by koa-proxy, it become a [object Object], So it can't proxy properly. And the request can't be sent.

1
votes

As @zeronone suggested, it is the body parser that causes the problem. Remove it and it'll work just fine.