Skip to content

Instantly share code, notes, and snippets.

@sanyamsmulay
Last active January 6, 2023 14:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sanyamsmulay/c4f9a3081c0416f474cba33f4c8319e1 to your computer and use it in GitHub Desktop.
Save sanyamsmulay/c4f9a3081c0416f474cba33f4c8319e1 to your computer and use it in GitHub Desktop.
gist for express server to serve some registered urls from self and others from proxy
const express = require('express');
const httpProxy = require('http-proxy');
const app = express();
const proxy = httpProxy.createProxyServer();
const port = 3001;
const serverBehindProxy = 'https://a.b.c';
// const serverBehindProxy = 'http://localhost:3000';
const pathsAvailableOnWebsite = {
"":true, // root path (http://localhost:3001/base-url-for-proxy; in this case) to be served from the website and not from the proxy
"path-available-on-website-1" :true, // to be served from website angular
"path-available-on-website-2" :true, // to be served from website angular
}
app.get('/', (req, res) => {
res.send('Hello World! From proxy server root');
});
// add middleware for interception
app.use('/base-url-for-proxy*', (req, res, next) => {
// uncomment for debugging
// console.log("middleware invoked");
// console.log('Request Type:', req.method);
// console.log('requested path:', req.path);
// console.log('requested baseUrl:', req.baseUrl); // url without params and stripped of the mount path
// console.log('requested originalUrl:', req.originalUrl);
// if path not reserved to be served from the website;
// proxy to wordpress server
const subDirAfterOCC = req.baseUrl.replace("/base-url-for-proxy", "").replace("/", ""); // cascaded to handle root path
if (!pathsAvailableOnWebsite[subDirAfterOCC]) {
// console.log("path not available on website, try to server from proxy");
// console.log(`trying target: ${serverBehindProxy}${req.originalUrl}`);
proxy.web(req, res, {
target: `${serverBehindProxy}${req.originalUrl}`,
// secure: false,
// toProxy: true, // what stupidity is this ??? the options do not work as documented
// prependPath: true,
ignorePath: true, // this needs to be true if express is setting the req.url to / for file req making them look like directory req
changeOrigin: true,
xfwd: true
}, (err) => {
console.log("error proxy serving the request :(", err);
});
} else {
console.log("path available on website, let angular serve it, do nothing");
next();
}
});
// app.get('/base-url-for-proxy*', (req, res) => {
// res.send('Hello World! From proxy server base path');
// });
app.get('*', (req, res) => {
res.send('Hello World! From catch-all path');
});
app.listen(port, () => {
console.log(`Example app listening on port ${port}`);
});
const express = require('express');
const app = express();
const port = 3000;
app.get('/', (req, res) => {
console.log("original url", req.originalUrl);
res.send('Hello World! From the base express server behind the proxy');
});
app.get('*', (req, res) => {
console.log("original url", req.originalUrl);
res.send('Hello World! From the catch-all path of base express server behind the proxy');
console.log("req headers:", req.headers);
});
app.listen(port, () => {
console.log(`Example app listening on port ${port}`);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment