I’m trying to make simple http server, that can be pause and resume,, I’ve looked at Nodejs API,, here http://nodejs.org/docs/v0.6.5/api/http.html
but that couldn’t help me,, I’ve tried to remove event listener on ‘request’ event and add back,, that worked well but the listen callback call increase every time i try to pause and resume,, here some code i did:
var httpServer = require('http').Server();
var resumed = 0;
function ListenerHandler(){
console.log('[-] HTTP Server running at 127.0.0.1:2525');
};
function RequestHandler(req,res){
res.writeHead(200,{'Content-Type': 'text/plain'});
res.end('Hello, World');
};
function pauseHTTP(){
if(resumed){
httpServer.removeAllListeners('request');
httpServer.close();
resumed = 0;
console.log('[-] HTTP Server Paused');
}
};
function resumeHTTP(){
resumed = 1;
httpServer.on('request',RequestHandler);
httpServer.listen(2525,'127.0.0.1',ListenerHandler);
console.log('[-] HTTP Server Resumed');
};
I don’t know quite what you’re trying to do, but I think you’re working at the wrong level to do what you want.
If you want incoming connection requests to your web server to block until the server is prepared to handle them, you need to stop calling the
accept(2)system call on the socket. (I cannot imagine that node.js, or indeed any web server, would make this task very easy. Therequestcallback is doubtless called only when an entire well-formed request has been received, well after session initiation.) Your operating system kernel would continue accepting connections up until the maximum backlog given to thelisten(2)system call. On slow sites, that might be sufficient. On busy sites, that’s less than a blink of an eye.If you want incoming connection requests to your web server to be rejected until the server is prepared to handle them, you need to
close(2)the listening socket. node.js makes this available via theclose()method, but that will tear down the state of the server. You’ll have to re-install the callbacks when you want to run again.