0

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?

dominik791
  • 692
  • 6
  • 17
  • 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 Feb 05 '17 at 11:14

2 Answers2

1

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.

X Rene
  • 419
  • 2
  • 7
1

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

Yigit Erol
  • 170
  • 1
  • 6