分类: LINUX
2013-06-02 17:17:44
如果要使用HTTP的服务器以及客户端,需使用require('http')加载HTTP模块。
The HTTP interfaces in Node are designed to support many features of the protocol which have been traditionally difficult to use. In particular, large, possibly chunk-encoded, messages. The interface is careful to never buffer entire requests or responses--the user is able to stream data.
This is an EventEmitter with the following events:
这是一个带有如下事件的EventEmitter事件触发器:
function (request, response) { }
request is an instance of http.ServerRequest and response is an instance of http.ServerResponse
Returns a new web server object.
The requestListener is a function which is automatically added to the 'request' event.
Begin accepting connections on the specified port and hostname. If the hostname is omitted, the server will accept connections directed to any IPv4 address (INADDR_ANY).
This object is created internally by a HTTP server -- not by the user -- and passed as the first argument to a 'request' listener.
function (chunk) { }
Emitted when a piece of the message body is received.
当接收到信息正文中的一部分时候会触发此事件。
Example: A chunk of the body is given as the single argument. The transfer-encoding has been decoded. The body chunk is a string. The body encoding is set with request.setBodyEncoding().
This object is created internally by a HTTP server--not by the user. It is passed as the second parameter to the 'request' event. It is a Writable Stream.
function (chunk) { }
Emitted when a piece of the message body is received.
当接收到信息正文中的一部分时候会触发此事件。
Example: A chunk of the body is given as the single argument. The transfer-encoding has been decoded. The body chunk is a string. The body encoding is set with request.setBodyEncoding().
If this method is called and response.writeHead() has not been called, it will switch to implicit header mode and flush the implicit headers.
Node maintains several connections per server to make HTTP requests. This function allows one to transparently issue requests.
Since most requests are GET requests without bodies, Node provides this convenience method. The only difference between this method and http.request() is that it sets the method to GET and calls req.end() automatically.
This object is created internally and returned from http.request(). It represents an in-progress request whose header has already been queued. The header is still mutable using the setHeader(name, value), getHeader(name), removeHeader(name) API. The actual header will be sent along with the first data chunk or when closing the connection.
function (response) { }
Emitted when a response is received to this request. This event is emitted only once. The response argument will be an instance of http.ClientResponse.