Chinaunix首页 | 论坛 | 博客
  • 博客访问: 323918
  • 博文数量: 103
  • 博客积分: 1590
  • 博客等级: 上尉
  • 技术积分: 1075
  • 用 户 组: 普通用户
  • 注册时间: 2009-12-02 10:17
文章分类

全部博文(103)

文章存档

2013年(32)

2012年(7)

2010年(64)

我的朋友

分类: LINUX

2013-06-02 17:17:44

HTTP HTTP模块


Table Of Contents

如果要使用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.

Node中的HTTP接口在设计时就考虑到了要支持HTTP协议的很多特性,并且使用简单。特别是可以处理那些内容庞大,有可能是块编码的消息。该接口被设计为从不缓冲整个请求或相应,这样用户就可以以流的方式处理数据。

http.Server

This is an EventEmitter with the following events:

这是一个带有如下事件的EventEmitter事件触发器:

Event: 'request' 事件:'request'

function (request, response) { }

request is an instance of http.ServerRequest and response is an instance of http.ServerResponse

request是http.ServerRequest的一个实例,而response是http.ServerResponse的一个实例。
http.createServer(requestListener)

Returns a new web server object.

The requestListener is a function which is automatically added to the 'request' event.

requestListener监听器会自动添加到'request'事件中。

server.listen(port, [hostname], [callback])

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).

在指定端口和主机名上接受连接。如果hostname没有指定,服务器将直接在此机器的所有IPV4地址上接受连接(INADDR_ANY)。

http.ServerRequest

This object is created internally by a HTTP server -- not by the user -- and passed as the first argument to a 'request' listener.

Event: 'data' 事件:'data'

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().

例如:正文的数据块将作为唯一的参数传递给回调函数。此时传输编码已被解码。正文数据块是一个字符串,正文的编码由request.setBodyEncoding()方法设定。

http.ServerResponse

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.

这个对象由HTTP服务器(而非用户)自动建立。它作为'request'事件的第二个参数,这是一个Writable Stream可写流。

Event: 'data' 事件:'data'

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().

例如:正文的数据块将作为唯一的参数传递给回调函数。此时传输编码已被解码。正文数据块是一个字符串,正文的编码由request.setBodyEncoding()方法设定。

response.write(chunk, encoding='utf8')

If this method is called and response.writeHead() has not been called, it will switch to implicit header mode and flush the implicit headers.

如果在response.writeHead()调用之前调用该函数,将会切换到隐式发送响应头信息的模式并发送隐式的头部信息。

http.request(options, callback)

Node maintains several connections per server to make HTTP requests. This function allows one to transparently issue requests.

Node为一个目标服务器维护多个连接用于HTTP请求。通过这个方法可以向服务器发送请求。

http.get(options, callback)

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.

由于大部分请求是不包含正文的GET请求,Node提供了这个方便的方法。与http.request()唯一的区别是此方法将请求方式设置为GET,并且自动调用req.end()。

http.ClientRequest

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.

这个对象是在调用http.request()时产生并返回的。它表示一个正在进行中且头部信息已经排列好了的请求。这时候通过setHeader(name, value),getHeader(name),removeHeader(name)这些API还可以改变头部信息,实际的头部信息将随着第一块数据发送,或者在关闭连接时发送出去。

Event 'response' 事件:'response'

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.

当请求的响应到达时触发,该事件仅触发一次。response参数是http.ClientResponse的一个实例。



阅读(1980) | 评论(0) | 转发(0) |
0

上一篇:nodejs file 模块

下一篇:MySQL 复制

给主人留下些什么吧!~~