views:

43

answers:

1

How can we listen to "http.Server" events? Or how can we instantiate a "http.Server" object?

According to nodejs.org/api.html:

"http.Server" is an EventEmitter with events like "request", "connection" .. etc.

However I can't find a way to listen to these event as I am not sure how to instantiate a "http.Server" object. All examples I can find are using "http.createServer" and the api return a new web server object instead of a EventEmitter object.

So, if I would like to know "connection" event, how should i do so?

A: 

You need to use net.CreateServer to create an instance of http.Server.

var server = net.CreateServer(callbackForTheConnectionEvent);
server.listen(1234, 'localhost');

Take a look at my code in this answer:
http://stackoverflow.com/questions/3965072/node-js-connecting-to-a-server-using-sockets

(just copying it wouldn't make much sense...)

Ivo Wetzel
Thanks for you clarification.Now I am wondering if it is possible to do :(net.createServer(connectionListener)).createServer(requestListener);
sglai
No, the server object has no further `createSever` method. What you probably want to do is, to setup the listeners for requests on the connection, inside the connectionListener.
Ivo Wetzel