Usage in Deno
import * as mod from "node:http";
To use the HTTP server and client one must import the node:http
module.
The HTTP interfaces in Node.js 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, so 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": "example.com", "accept": "*" }
Keys are lowercased. Values are not modified.
In order to support the full spectrum of possible HTTP applications, the Node.js 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.
See message.headers
for details on how duplicate headers are handled.
The raw headers as they were received 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', 'example.com', 'accepT', '*' ]
An Agent
is responsible for managing connection persistenceand reuse for HTTP clients. It maintains a queue of pending requestsfor a given host and port, reusing a single socket connection for eachuntil the queue is empty, at which time the socket is either destroyedor put into a pool where it is kept to be used again for requests to thesame host and port. Whether it is destroyed or pooled depends on the keepAlive
option
.
This object is created internally and returned from request. Itrepresents an in-progress request whose header has already been queued. Theheader is still mutable using the setHeader(name, value)
, getHeader(name)
, removeHeader(name)
API. The actual header willbe sent along with the first data chunk or when calling request.end()
.
An IncomingMessage
object is created by Server or ClientRequest and passed as the first argument to the 'request'
and 'response'
event respectively. It may be used toaccess responsestatus, headers, and data.
This class serves as the parent class of ClientRequest and ServerResponse. It is an abstract outgoing message fromthe perspective of the participants of an HTTP transaction.
This object is created internally by an HTTP server, not by the user. It ispassed as the second parameter to the 'request'
event.
Returns a new instance of Server.
Since most requests are GET requests without bodies, Node.js provides thisconvenience method. The only difference between this method and request is that it sets the method to GET by default and calls req.end()
automatically. The callback must take care toconsume the responsedata for reasons stated in ClientRequest section.
options
in socket.connect()
are also supported.
Set the maximum number of idle HTTP parsers.
Performs the low-level validations on the provided name
that are done when res.setHeader(name, value)
is called.
Performs the low-level validations on the provided value
that are done when res.setHeader(name, value)
is called.
- accept
- accept-language
- accept-patch
- accept-ranges
- access-control-allow-credentials
- access-control-allow-headers
- access-control-allow-methods
- access-control-allow-origin
- access-control-expose-headers
- access-control-max-age
- access-control-request-headers
- access-control-request-method
- age
- allow
- alt-svc
- authorization
- cache-control
- connection
- content-disposition
- content-encoding
- content-language
- content-length
- content-location
- content-range
- content-type
- cookie
- date
- etag
- expect
- expires
- forwarded
- from
- host
- if-match
- if-modified-since
- if-none-match
- if-unmodified-since
- last-modified
- location
- origin
- pragma
- proxy-authenticate
- proxy-authorization
- public-key-pins
- range
- referer
- retry-after
- sec-websocket-accept
- sec-websocket-extensions
- sec-websocket-key
- sec-websocket-protocol
- sec-websocket-version
- set-cookie
- strict-transport-security
- tk
- trailer
- transfer-encoding
- upgrade
- user-agent
- vary
- via
- warning
- www-authenticate
- accept
- accept-charset
- accept-encoding
- accept-language
- accept-ranges
- access-control-allow-credentials
- access-control-allow-headers
- access-control-allow-methods
- access-control-allow-origin
- access-control-expose-headers
- access-control-max-age
- access-control-request-headers
- access-control-request-method
- age
- allow
- authorization
- cache-control
- cdn-cache-control
- connection
- content-disposition
- content-encoding
- content-language
- content-length
- content-location
- content-range
- content-security-policy
- content-security-policy-report-only
- cookie
- date
- dav
- dnt
- etag
- expect
- expires
- forwarded
- from
- host
- if-match
- if-modified-since
- if-none-match
- if-range
- if-unmodified-since
- last-modified
- link
- location
- max-forwards
- origin
- prgama
- proxy-authenticate
- proxy-authorization
- public-key-pins
- public-key-pins-report-only
- range
- referer
- referrer-policy
- refresh
- retry-after
- sec-websocket-accept
- sec-websocket-extensions
- sec-websocket-key
- sec-websocket-protocol
- sec-websocket-version
- server
- set-cookie
- strict-transport-security
- te
- trailer
- transfer-encoding
- upgrade
- upgrade-insecure-requests
- user-agent
- vary
- via
- warning
- www-authenticate
- x-content-type-options
- x-dns-prefetch-control
- x-frame-options
- x-xss-protection
Global instance of Agent
which is used as the default for all HTTP clientrequests. Diverges from a default Agent
configuration by having keepAlive
enabled and a timeout
of 5 seconds.
Read-only property specifying the maximum allowed size of HTTP headers in bytes.Defaults to 16KB. Configurable using the --max-http-header-size
CLI option.