Logging Proxy Requests in Express | Generated by AI

Home 2025.09

If you’re using http-proxy-middleware in an Express app, you can hook into its onProxyReq and onProxyRes events to log request parameters and body.

Here’s a practical example:

const express = require('express');
const { createProxyMiddleware } = require('http-proxy-middleware');
const app = express();

// Parse JSON body before proxy
app.use(express.json());
app.use(express.urlencoded({ extended: true }));

app.use('/api', createProxyMiddleware({
  target: 'http://example.com',
  changeOrigin: true,

  // Log the request body & query params
  onProxyReq: (proxyReq, req, res) => {
    // Query params
    console.log('Query:', req.query);

    // Body (only if parsed before proxy)
    if (req.body && Object.keys(req.body).length) {
      console.log('Body:', req.body);

      // If you need to forward the body (for POST/PUT)
      const bodyData = JSON.stringify(req.body);
      proxyReq.setHeader('Content-Type', 'application/json');
      proxyReq.setHeader('Content-Length', Buffer.byteLength(bodyData));
      proxyReq.write(bodyData);
    }
  },

  // Log the response from target
  onProxyRes: (proxyRes, req, res) => {
    console.log(`Response from target: ${proxyRes.statusCode} for ${req.method} ${req.originalUrl}`);
  }
}));

app.listen(3000, () => {
  console.log('Proxy running on http://localhost:3000');
});

Key Points


👉 Do you just need logging for debugging, or do you also want the proxy to forward the modified body after logging?


Back Donate