How to proxy data without transfer-encoding: chunked?

Viewed 367

I have an express.js-based proxy that I use to proxy various separate apps deployed via Serverless and AWS Lambda into the same domain:

const serverless = require('serverless-http');
const httpProxy = require('http-proxy');
const express = require('express');


const app = express();

const proxy = httpProxy.createProxyServer();

proxy.on('proxyReq', (proxyReq) => {
  proxyReq.setHeader('X-Projects-Router-Proxy-Out', true);
});

app.get(['/:project', '/:project/*'], (req, res) => {
  const { project } = req.params;
  const rest = req.params[0];
  const url = `https://${project}.${process.env.DEPLOY_STAGE}.example.com/${project}`;
  req.url = rest ? `/${rest}` : '';

  proxy.web(req, res, {
    target: url,
    xfwd: false,
    toProxy: true,
    changeOrigin: true,
    secure: true,
  });
});

module.exports.handler = serverless(app);

This works very well for most things, but I'm now having trouble. One of the projects I'm proxying is a next.js app, and the _next directory's assets all return a 502 Bad Gateway error because chunked encoding is not supported by AWS API Gateway (which I'm using to handle all the requests). According to some google results, I ought to just be able to removed the transfer-encoding: chunked header.

Which would be fine, except there is no transfer-encoding header being sent on these assets, so removing it does nothing to fix the problem. I think there is also no content-length header being sent, so it assumes chunked by default. How do I add a content-length header? Is there another way of getting around this problem?

0 Answers
Related