Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Node http-proxy and express

Tags:

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'));
});
like image 783
BRogers Avatar asked Dec 06 '13 19:12

BRogers


People also ask

What does express http-proxy do?

Express middleware to proxy request to another host and pass response back to original caller.

What is node http-proxy?

node-http-proxy is an HTTP programmable proxying library that supports websockets. It is suitable for implementing components such as reverse proxies and load balancers.

Are node and express the same?

NodeJS is an event-driven, non-blocking I/O model using JavaScript as its main language. It helps to build scalable network applications. Express is a minimal and flexible Node. js web application framework that provides a robust set of features for web and mobile applications.

What is node and Express?

Node. js is a platform for building the i/o applications which are server-side event-driven and made using JavaScript. Express. js is a framework based on Node. js for which is used for building web-application using approaches and principles of Node.


2 Answers

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' });
});
like image 63
Chandler Avatar answered Oct 10 '22 04:10

Chandler


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.

like image 25
Selfish Avatar answered Oct 10 '22 06:10

Selfish