Search This Blog


Node Session Management using memcached and run Proxy Server as Windows Service

To Install as a windows service:
npm install winser --save
package.json should contain "start": "node proxy-server.js"
run C:\Manab\Education\nodedemo\demomultiprocessor\node_modules\.bin\winser -i and check services.msc->proxy_demo->and start the service
open three command prompt and type 'node server.js 8081','node server.js 8082',node server.js 8083' respectively in 3 cmd prmpt and call proxy by http://localhost:8080.We can see response from 3 node server for each request.

Uninstall windows service :
C:\Manab\Education\nodedemo\demomultiprocessor\node_modules\.bin\winser -r

Session management in node when running on multiple server:
Downaload memcached in C:\Manab\Education\nodedemo\demomultiprocessor\memcached from and
run following command to install as a windows local service (as administrator)
C:\Manab\Education\nodedemo\demomultiprocessor\memcached>memcached -d install
run below command to start the service
C:\Manab\Education\nodedemo\demomultiprocessor\memcached>net start memcached

Set memory and port for memcached :
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\memcached=>imagepath="C:\Manab\Education\nodedemo\demomultiprocessor\memcached\memcached.exe" -d runservice -m 10 -p 12345
install memcached modules
npm install memcached --save
and use the imagepart port while creating the object of memcached in server.js
var Memcached = require('memcached');
var memcached = new Memcached('localhost:12345');

Code :

server_list.json :

 "servers" :[

Package.json :

  "name": "proxy_demo",
  "version": "1.0.0",
  "description": "demo the proxy",
  "main": "index.js",
  "scripts": {
    "start": "node proxy-server.js"
  "author": "",
  "license": "ISC",
  "dependencies": {
    "http-proxy": "0.8.x",
    "memcached": "^2.2.1",
    "winser": "^1.0.2"
  "start": "node proxy-server.js"

server.js :

var http=require("http");
var Memcached = require('memcached');
var memcached = new Memcached('localhost:12345');
var lifetime = 86400;
var lastport='';
console.log("Creating server");
var s=http.createServer(function(req,res){
 console.log("listing on "+process.argv[2]);
 memcached.get('lastport', function( err, result ){
  if( err ) console.error( err );
   console.dir('reading lastport'+ lastport );
   res.end("I am listing on port:" + process.argv[2] + "and last port is " +lastport);
 memcached.set('lastport', process.argv[2], lifetime, function( err, result ){
  if( err ) console.error( err );
  console.dir( result );
 //res.end("I am listing on port:" + process.argv[2]);

proxy-server.js :

var http_proxy=require("http-proxy");
var fs=require("fs");
var servers=JSON.parse(fs.readFileSync('server_list.json')).servers;
var s=http_proxy.createServer(function(req,res,proxy){
var target=servers.shift();

run: node proxy-server.js

Open browser and try http://localhost:8080

No comments: