Skip to content

Instantly share code, notes, and snippets.

@lifeofcoding
Forked from davemo/api.proxy.server.js
Created January 16, 2019 05:01
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 lifeofcoding/307333043ac71c1aa89592004131439a to your computer and use it in GitHub Desktop.
Save lifeofcoding/307333043ac71c1aa89592004131439a to your computer and use it in GitHub Desktop.
A simple express.js server with a proxy that intercepts all requests with /api/ and proxies them to localhost:3000
var express = require('express'),
httpProxy = require('http-proxy'),
app = express();
var proxy = new httpProxy.RoutingProxy();
function apiProxy(host, port) {
return function(req, res, next) {
if(req.url.match(new RegExp('^\/api\/'))) {
proxy.proxyRequest(req, res, {host: host, port: port});
} else {
next();
}
}
}
app.configure(function() {
app.use(express.static(process.cwd() + "/generated"));
app.use(apiProxy('localhost', 3000));
app.use(express.bodyParser());
app.use(express.errorHandler());
});
module.exports = app;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment