Reputation: 3604
I'm trying to do something like this:
// Setup prox to handle blog requests
httpProxy.createServer({
hostnameOnly: true,
router: {
'http://localhost': '8080',
'http://localhost/blog': '2368'
}
}).listen(8000);
Previously I was using this:
http.createServer(app).listen(app.get('port'), function(){
console.log("Express server listening on port " + app.get('port'));
});
Basically, I want to still use express... but, when people go to http://localhost/blog
get taken to the blog but still be served over port 8080
(which will eventually be port 80)
So I switched it to this and it worked better. The problem is that express takes over the routing (from what I can tell)
var options = {
// pathnameOnly: true,
router: {
'localhost': 'localhost:8080',
'localhost/blog': 'localhost:2368'
}
}
// Setup prox to handle blog requests
var proxyServer = httpProxy.createServer(options);
proxyServer.listen(9000);
require('./app/server/router')(app);
http.createServer(app).listen(app.get('port'), function(){
console.log("Express server listening on port " + app.get('port'));
});
Upvotes: 20
Views: 38987
Reputation: 806
I have used simple solution to proxified my GET/POST requests.
var httpProxy = require('http-proxy');
var apiProxy = httpProxy.createProxyServer();
app.post("/api/*", function(req, res) {
apiProxy.web(req, res, { target: 'http://localhost:5000'})
});
app.get("/api/*", function(req, res) {
apiProxy.web(req, res, { target: 'http://localhost:5000'})
});
another easier way to handle all type of requests is:
app.all("/api/*", function(req, res) {
apiProxy.web(req, res, { target: 'http://localhost:5000'})
});
NOTE: above functions must be before bodyparser.
Upvotes: 2
Reputation: 6190
A very straightforward solution which works seamlessly, and with cookies/authentication as well, using express-http-proxy
:
var proxy = require('express-http-proxy');
var blogProxy = proxy('localhost/blog:2368', {
forwardPath: function (req, res) {
return require('url').parse(req.url).path;
}
});
And then simply:
app.use("/blog/*", blogProxy);
I know I'm late to join this party, but I hope this helps someone.
Upvotes: 9
Reputation: 1078
Using http-proxy 1.0 with express:
var httpProxy = require('http-proxy');
var apiProxy = httpProxy.createProxyServer();
app.get("/api/*", function(req, res){
apiProxy.web(req, res, { target: 'http://google.com:80' });
});
Upvotes: 44
Reputation: 116
I got this working.
npm install http-proxy
in your web appCreate wildcard route for /blog* that proxies requests to Ghost service
var httpProxy = require('http-proxy');
var proxy = new httpProxy.RoutingProxy();
app.get('/blog*', function (req, res, next) {
proxy.proxyRequest(req, res ,{
host: 'moserlap.splitvr.com',
port: 2368
});
});
Update the Ghost config to use a sub directory (only supported in 0.4.0+)
config = {
// ### Development **(default)**
development: {
// The url to use when providing links to the site, E.g. in RSS and email.
url: 'http://127.0.0.1/blog',
...
You should now be able to hit http://yoursite.com/blog and all routes work.
Upvotes: 4