Created
April 27, 2011 21:43
-
-
Save indexzero/945283 to your computer and use it in GitHub Desktop.
Conditional URL rewrite forwarding
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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