Skip to content

Instantly share code, notes, and snippets.

@pjkelly
Created October 27, 2017 00:12
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 pjkelly/a73072b8279825e7cb312c856254bdaf to your computer and use it in GitHub Desktop.
Save pjkelly/a73072b8279825e7cb312c856254bdaf to your computer and use it in GitHub Desktop.
Sample express app that acts as a proxy with CORS support.
const express = require('express');
const cors = require('cors');
const proxy = require('http-proxy-middleware');
const app = express();
const port = process.env.PORT || 8080;
const onProxyReq = function (proxyReq, req, res) {
proxyReq.setHeader('Authorization', 'Basic SECRET');
};
const apiProxy = proxy('**', {
target: 'THE_URL_YOU_ARE_PROXYING_TO',
changeOrigin: true, // for vhosted sites
onProxyReq: onProxyReq
});
app.use(cors());
app.use(apiProxy);
app.listen(port, function() {
console.log('Proxy app is running on http://localhost:' + port);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment