简体   繁体   English

node.js的新增功能-“ http.Server”事件

[英]new to node.js - “http.Server” events

How can we listen to "http.Server" events? 我们如何收听“ http.Server”事件? Or how can we instantiate a "http.Server" object? 或者我们如何实例化“ http.Server”对象?

According to nodejs.org/api.html: 根据nodejs.org/api.html:

"http.Server" is an EventEmitter with events like "request", "connection" .. etc. “ http.Server”是一个EventEmitter,具有诸如“ request”,“ connection”等事件。

However I can't find a way to listen to these event as I am not sure how to instantiate a "http.Server" object. 但是,由于不确定如何实例化“ http.Server”对象,因此无法找到侦听这些事件的方法。 All examples I can find are using "http.createServer" and the api return a new web server object instead of a EventEmitter object. 我可以找到的所有示例都使用“ http.createServer”,并且api返回一个新的Web服务器对象,而不是EventEmitter对象。

So, if I would like to know "connection" event, how should i do so? 因此,如果我想知道“连接”事件,该怎么办?

You need to use net.CreateServer to create an instance of http.Server . 您需要使用net.CreateServer创建的实例http.Server

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

Take a look at my code in this answer: 看看我在此答案中的代码:
Node.js: Connecting to a Server Using Sockets Node.js:使用套接字连接到服务器

(just copying it wouldn't make much sense...) (只是复制它没有多大意义...)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM