Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Node Reverse Proxy with Express
var fs = require('fs');
var express = require('express');
var request = require('request');
var apiUrl = 'http://example.com'; // remote host
var app = express();
// replace specific requests
app.get('/Scripts/js.js', function(req, res, next) {
console.log(req.url, req.query, 'script modified!');
fs.createReadStream(__dirname+'/js.js').pipe(res);
});
app.use(function(req, res) {
console.log(req.url, req.query);
var url = apiUrl + req.url;
var proxy = request({
method: req.method,
url: url,
qs: req.query,
headers: req.headers
});
req.pipe(proxy);
proxy.pipe(res);
});
var server = app.listen(8575); // change port
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.