Skip to content

Instantly share code, notes, and snippets.

@kannanhassouna
Created August 2, 2025 20:14
Show Gist options
  • Select an option

  • Save kannanhassouna/c643ca9fa73aa84f0bedb70a20fdf5a3 to your computer and use it in GitHub Desktop.

Select an option

Save kannanhassouna/c643ca9fa73aa84f0bedb70a20fdf5a3 to your computer and use it in GitHub Desktop.
const express = require("express");
const { createProxyMiddleware } = require("http-proxy-middleware");
// app definition
const app = express();
// laod cors in the dev environment
const cors = require("cors");
app.use(cors());
// Load environment variables
const TARGET_URL = "http://localhost:3000"; // gets loaded from the env file
app.use((req, res, next) => {
console.log(`req.path: ${req.path}, req.ip: ${req.ip}`);
next();
});
app.use(
createProxyMiddleware({
target: TARGET_URL,
changeOrigin: true,
logger: console,
secure: false,
onProxyReq: (proxyReq, req, res) => {},
onError: (err, req, res) => {
const status = err.status || 503; // Default to 503 if err.status is not defined
const message = `Proxy error: ${req.url} - ${
err.message || "Internal Server Error"
}`;
res.status(status).send(message);
},
})
);
// running on default port
const PORT = 4000;
app.listen(PORT, () => {
console.log(`Proxy server is running on port ${PORT}`);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment