Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save tomeast/952924 to your computer and use it in GitHub Desktop.
Save tomeast/952924 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();
//will match the root url or
//a request for any html pages
var apacheUrls = [
/.*\.html/,
/^\/$/
];
//
// Create a regular http server and use the `proxy`
// conditionally inside the request handler
//
http.createServer(function (req, res) {
//return true if any of the apacheUrls match
var shouldProxy = apacheUrls.some(function (url) {
return url.test(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(8000);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment