HTTP
To use the HTTP server and client one must
require('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 nerver buffer entire requests or responses--the user is able to stream data.
HTTP message headers are represented by an object like this:
{ 'content-length': '123',
'content-type': 'text/plain',
'connection': 'keep-alive',
'host': 'mysite.com',
'accept': '*/*' }
Keys are lowercased.Values are not modified.
In order to support the full spectrum of possible HTTP applications,Node's HTTP API is very low-level.It deals with stream handling and message parsing only.It parses a message into headers and body but it dose not parse the actual headers or the body.
Defined headers that allow multiple values are concatenated with a
, character,except for the
set-cookie and
cookie headers which are represented as an arry of values.Headers such as
content-length which can only have a single value are parsed accordingly,and only a single value is represented on the parsed object.
The raw headers as they were recevied are retained in the
rawHeaders property,which is an array of
[key, value, key2, value2, ...].For example,the previous message header object might have a
rawHeaders list like the following:
[ 'ConTent-Length', '123456',
'content-LENGTH', '123',
'content-type', 'text/plain',
'CONNECTION', 'keep-alive',
'Host', 'mysite.com',
'accepT', '*/*' ]
http.METHODS
A list of the HTTP methods that are supported by the parser.
http.STATUS_CODES
A collection of all the standard HTTP response status codes, and the short description of each. For example,
http.STATUS_CODES[404] === 'Not Found'.
http.createServer([requestListener])
Returns a new instance of http.Server
The
requestListener is a function which is automatically added to the
'request' event.
http.createClient([port][,host])
This function
deprecated;please use
http.request() instead.Constructs a new HTTP client.
port and
host refer to the server to be connected to.
Class: http.Server
This is an
EventEmitter with the following events:
Event: 'request'
function (request, response) { }
Emitted each time there is a request.Note that there may be multiple requests per connection(in the case of keep-alive connection).
request is an instance of
http.IncomingMessage and
response is an instance of
http.ServerResponse.
Event: 'connection'
When a new TCP stream is established.
socket is an object of type
net.Socket.Usually users will not want to access this event.In particular,the socket will not emit
readable events because of how the protocol parser attaches to the
socket.The socket can also be accessed at
request.connection.
Event: 'close'
Emitted when the server closes.
Event: 'checkContinue'
function (request, response) { }
Emitted each time a request with an http Expect: 100-continue is received.If this event isn't listened for,the server will automatically respond with a 100 Continue as appropriate.
Handling this event involes calling
response.writeContinue() if the client should continue to send the request body, or generating an appropriate HTTP response(e.g.,400 Bad Request) if the client should not continue to send the request body.
Note that when this event is emitted and handled,the
request event will not be emitted.
Event: 'connect'
function (request, socket, head) { }