-
-
Save tomeast/952924 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(); | |
//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