a blog for those who code

Tuesday 15 July 2014

Proxies in Nodejs

In this post we are going to learn that how we can make use of proxy in Nodejs application. A proxy is a way of routing requests from several different sources through one server whether its for cahing, security or anything else.

Suppose for an example, some pages which are not publicly accessible and someone try to access that, the page will be redirected to some other page. This type of proxy is also called a forward proxy.

A reverse proxy is a way of controlling how requests are sent to a server. For example if a website is accessible by millions of user then to balance load some of the requests will be redirected to some specific server, thus improving the overall performance of the system.

In node the most important proxy module is http-proxy.
Lets look into an example of using http-proxy :

var http = require('http');
var httpProxy = require('http-proxy');

httpProxy.createServer(3247, 'localhost').listen(3030);

http.createServer(function( req, res) {

res.writeHead(200, {'Content-Type' : 'text/plain' });
res.write('request proxied');

This simple application will listen for requests on port 3030 and proxy them to HTTP server listening on port 3247.

Please Like and Share the Blog, if you find it interesting and helpful.

No comments:

Post a Comment