Node.js net socket pipe to http

Description

Node.js net socket pipe to http


var net=require('net'), 
    http=require('http'), 
    sockets=[];/*  w ww. j a v  a2  s .  co m*/

var getSocket=function(){
  while(sockets.length){
    var s=sockets.shift();
    if (s.readable) { return s; }
  }
  return null;
};

var s=net.createServer(function(socket){
  sockets.push(socket);
});
s.listen(8000);

var h=http.createServer(function(req, res){
  var s=getSocket();
  if (s){
    s.pipe(res);
  }else{
    res.end('no socket available');
  }
});
h.listen(8001);



PreviousNext

Related