THE WORLD'S LARGEST WEB DEVELOPER SITE

Node.js HTTP Module

❮ Built-in Modules


Example

Create a server that listens on port 8080 of your computer.

When port 8080 get accessed, write "Hello World!" back as a response:

var http = require('http');
http.createServer(function (req, res) {
  res.writeHead(200, {'Content-Type': 'text/plain'});
  res.write('Hello World!');
  res.end();
}).listen(8080);
Run example »

Defintion and Usage

The HTTP module provides a way of making Node.js transfer data over HTTP (Hyper Text Transfer Protocol).


Syntax

The syntax for including the HTTP module in your application:

var http = require('http');

HTTP Properties and Methods

Method Description
createClient() Deprecated. Creates a HTTP client
createServer() Creates an HTTP server
get() Sets the method to GET, and returns an object containing the user's request
globalAgent Returns the HTTP Agent
request() Returns an object containing the user's request

HTTP Server Class

When calling the http.createServer() method, a Server class is returned.

The Server class has methods and properties of its own:

Server Methods and Properties

server.close() Closes the server connection
server.listen() Makes the server listen to ports on the computer
server.listening Returns true if the server is listening for connection, otherwise false
server.maxHeadersCount Limits the number of incoming headers
server.setTimeout() Sets the server's timeout value. Default is 2 minutes
server.timeout Sets, or gets, the server's timeout value

Server Events

checkContinue Occurs when a request with an HTTP Expect: 100-continue is received
checkExpectation Occurs when a request with an HTTP Expect headers is received
clientError Occurs when a client connection gets an error
close Occurs when the server is closing
connect Occurs when a request with an HTTP Connect method is received
connection  
request Occurs when a request is received
upgrade Occurs when a request for an HTTP upgrade is received

HTTP ServerResponse Class

This object is passed as the second parameter to the request event

The ServerResponse class has methods and properties of its own:

ServerResponse Methods and Properties

response.addTrailers() Adds trailing headers at the end of the response
response.end() The response message is completed
response.finished True when the response has completed, otherwise false
response.getHeader() Returns the specified HTTP header
response.headersSent True if any headers has been sent, otherwise false
response.removeHeader() Removes the specified HTTP header
response.sendDate If set to true, a Date header will be sent with the response
response.setHeader() Sets the specified HTTP header
response.setTimeout Sets the maximum number of milliseconds of the response
response.statusCode Sets the HTTP status code of the response
response.statusMessage Sets the HTTP status message of the response
response.write() Sends the specified string content to the response body
response.writeContinue() Sends a HTTP 100-Continue message
response.writeHead() Sends a response header

ServerResponse Events

close Occurs if the response was terminated before it could flush
finish Occurs when the response has been sent

HTTP ClientRequest Class

This object is passed as the first parameter to the request event.

The ClientRequest class has methods and properties of its own:

ClientRequest Methods and Properties

request.abort() Aborts the request
request.end() Ends the request
request.flushHeaders() Sends the request headers
request.setNoDelay()  
request.setSocketKeepAive()  
request.setTimeout()  
request.write()  

ClientRequest Events

abort Occurs when the request has been aborted by the client
aborted Occurs when the request has been aborted by the server
connect Occurs when a request with a Connect method has been received
continue Occurs when a 100 Continue response is received
response Occurs when the request receives a response
socket Occurs when a request gets a socket
upgrade Occurs when a request for an HTTP upgrade is received

HTTP IncomimngMessage Class

This object is created by the Server class or the ClentReuest class, and is passed as the first argument to the response and request events.

The IncomingMessage class has methods and properties of its own:

IncomingMessage Methods and Properties

message.destroy() Destroys the incoming message
message.headers Returns the header object
message.httpVersion Returns the HTTP version
message.method Returns the HTTP method
message.rawHeaders Returns the HTTP headers
message.rawTrailers Returns an array of the HTTP trailers
message.setTimeout()  
message.statusCode Returns the status code
message.statusMessage Returns the status message
message.socket Returns the socket that belongs to this connection
message.trailers Returns the HTTP trailers
message.url Returns the request url

IncomingMessage Events

aborted Occurs when the request has been aborted by the client
close Occurs when the connection is closing

 HTTP Agent Class

This object is created by the ClientRequest class.

The Agent class has methods and properties of its own:

IncomingMessage Methods and Properties

agent.createConnection() Creates a HTTP client
agent.destroy() Creates an HTTP server
agent.freeSockets Returns an object containing the sockets waiting
agent.getName() Sets the method to GET, and returns an object containing the user's request
agent.maxFreeSockets Sets the maximum number of free sockets. Default is 256
agetn.maxSockets Sets the maximum allowed number of sockets per origin.
agent.requests Returns the requests that has not been assign to any socket
agent.sockets Returns an object containing the sockets currently used

❮ Built-in Modules