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.

HTTP message headers are represented by an object like this:

{ 'content-length': '123',
  'content-type': 'text/plain',
  'connection': 'keep-alive' }

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 does not parse the actual headers or the body.

Static variables

staticMETHODS:Array<String>

A list of the HTTP methods that are supported by the parser.

staticread onlySTATUS_CODES:DynamicAccess<String>

A collection of all the standard HTTP response status codes, and the short description of each. For example, http.STATUS_CODES[404] === 'Not Found'.

staticglobalAgent:Agent

Global instance of Agent which is used as the default for all http client requests.

Static methods

staticcreateServer(?requestListener:(request:IncomingMessage, response:ServerResponse) ‑> Void):Server

staticcreateServer(options:HttpCreateServerOptions, ?requestListener:(request:IncomingMessage, response:ServerResponse) ‑> Void):Server

Returns a new web server object.

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

staticget(options:HttpRequestOptions, ?callback:IncomingMessage ‑> Void):ClientRequest

staticget(url:URL, ?options:Null<HttpRequestOptions>, ?callback:IncomingMessage ‑> Void):ClientRequest

staticget(url:String, ?options:Null<HttpRequestOptions>, ?callback:IncomingMessage ‑> Void):ClientRequest

Since most requests are GET requests without bodies, Node.js provides this convenience method. The only difference between this method and request() is that it sets the method to GET and calls end() automatically. The callback must take care to consume the response data for reasons stated in http.ClientRequest section.

staticrequest(options:HttpRequestOptions, ?callback:IncomingMessage ‑> Void):ClientRequest

staticrequest(url:URL, ?options:Null<HttpRequestOptions>, ?callback:IncomingMessage ‑> Void):ClientRequest

staticrequest(url:String, ?options:Null<HttpRequestOptions>, ?callback:IncomingMessage ‑> Void):ClientRequest

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

url can be a string or a URL object. If url is a string, it is automatically parsed with new URL(). If it is a URL object, it will be automatically converted to an ordinary options object.

If both url and options are specified, the objects are merged, with the options properties taking precedence.

The optional callback parameter will be added as a one-time listener for the 'response' event.

request() returns an instance of the http.ClientRequest class. The ClientRequest instance is a writable stream. If one needs to upload a file with a POST request, then write to the ClientRequest object.