Skip to content

Instantly share code, notes, and snippets.

@indexzero
Created April 27, 2011 21:43
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save indexzero/945283 to your computer and use it in GitHub Desktop.
Save indexzero/945283 to your computer and use it in GitHub Desktop.
Conditional URL rewrite forwarding
var http = require('http'),
httpProxy = require('http-proxy');
//
// Create a new instance of HttProxy to use in your server
//
var proxy = new httpProxy.HttpProxy();
var apacheUrls = [
/some\/url\/as\/a\/regexp/,
/some\/other\/url\/as\/a\/regexp/
];
//
// Create a regular http server and use the `proxy`
// conditionally inside the request handler
//
http.createServer(function (req, res) {
var shouldProxy = apacheUrls.every(function (url) {
return url.match(req.url);
});
if (shouldProxy) {
//
// If `req.url` matches any of the `apacheUrls` then
// proxy to Apache.
//
return proxy.proxyRequest(req, res, {
//
// This is the location of your Apache server
//
host: 'localhost',
port: 9000
});
}
//
// Otherwise, do run whatever node.js logic you want here.
//
}).listen(8001);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment