Migrate to Netlify Today

Netlify announces the next evolution of Gatsby Cloud. Learn more

Proxy — Reflect 4

(For Node.js http module)

req.on('end', () => const reflectedData = type: 'reflect_4_proxy', method: req.method, url: req.url, headers: req.headers, body: Buffer.concat(body).toString(), protocol: req.socket.encrypted ? 'https' : 'http' ;

// Increase limit in your server creation const server = http.createServer(app); server.maxHeadersCount = 0; // Unlimited // Use body-parser with increased limit if using Express app.use(express.json( limit: '50mb' )); Cause: The reflect proxy tries to mirror encrypted traffic without decrypting it. Solution: Terminate TLS at the proxy level. reflect 4 proxy

If the pre-built package does not fit your needs, writing a custom Reflect Proxy using the http module and http-proxy library is straightforward. Version 4 of the Node.js http-proxy library supports native reflect events. Step 1: Initialize the project mkdir reflect4-proxy && cd reflect4-proxy npm init -y npm install http-proxy@4 Step 2: The Reflect Logic The key to a "reflect proxy" is listening for the proxyReq event, capturing the data, and sending it back to the client without necessarily forwarding it to the target server.

openssl req -x509 -newkey rsa:4096 -keyout key.pem -out cert.pem -days 365 -nodes Then configure your proxy to use these certificates. Cause: The proxy reflects a request that was itself reflected by another proxy. Solution: Add a unique X-Reflect-Count header and stop reflecting when the count exceeds a threshold (e.g., 3). (For Node

const reflectCount = parseInt(req.headers['x-reflect-count'] || '0'); if (reflectCount >= 3) res.writeHead(508, 'Content-Type': 'text/plain' ); res.end('Loop Detected'); return;

Client -> [Reflect Proxy v4] -> Production Server (Response to Client) | +---------------> Staging Server (Silent, Async) If the pre-built package does not fit your

const proxy = httpProxy.createProxyServer( target: 'http://production' ); proxy.on('proxyRes', (proxyRes, req, res) => // Clone the request for staging (Reflection) const stagingReq = http.request( host: 'staging.internal', path: req.url, method: req.method, headers: req.headers ); req.pipe(stagingReq); stagingReq.end(); ); Version 4 standards often require adding tracing headers to see the round trip. Inject X-Reflected-By and X-Reflect-ID .