home / documentation / v19 / https

HTTPS

目录

Added in: v0.10.0

稳定性: 2 - Stable

HTTPS is the HTTP protocol over TLS/SSL. In Node.js this is implemented as a separate module.

Determining if crypto support is unavailable

It is possible for Node.js to be built without including support for the node:crypto module. In such cases, attempting to import from https or calling require('node:https') will result in an error being thrown.

When using CommonJS, the error thrown can be caught using try/catch:

CJS

When using the lexical ESM import keyword, the error can only be caught if a handler for process.on('uncaughtException') is registered before any attempt to load the module is made (using, for instance, a preload module).

When using ESM, if there is a chance that the code may be run on a build of Node.js where crypto support is not enabled, consider using the import() function instead of the lexical import keyword:

MJS

C https.Agent

历史
版本更改
v5.3.0support `0` `maxCachedSessions` to disable TLS session caching.
v2.5.0parameter `maxCachedSessions` added to `options` for TLS sessions reuse.
v0.4.5Added in: v0.4.5

An Agent object for HTTPS similar to http.Agent. See https.request() for more information.

M new Agent([options])

  • options Object Set of configurable options to set on the agent. Can have the same fields as for http.Agent(options), and

    • maxCachedSessions number maximum number of TLS cached sessions. Use 0 to disable TLS session caching. Default: 100.

    • servername string the value of Server Name Indication extension to be sent to the server. Use empty string '' to disable sending the extension. Default: host name of the target server, unless the target server is specified using an IP address, in which case the default is '' (no extension).

      See Session Resumption for information about TLS session reuse.

E 'keylog'

Added in: v13.2.0, v12.16.0

  • line Buffer Line of ASCII text, in NSS SSLKEYLOGFILE format.
  • tlsSocket tls.TLSSocket The tls.TLSSocket instance on which it was generated.

The keylog event is emitted when key material is generated or received by a connection managed by this agent (typically before handshake has completed, but not necessarily). This keying material can be stored for debugging, as it allows captured TLS traffic to be decrypted. It may be emitted multiple times for each socket.

A typical use case is to append received lines to a common text file, which is later used by software (such as Wireshark) to decrypt the traffic:

JS

C https.Server

Added in: v0.3.4

See http.Server for more information.

M server.close([callback])

Added in: v0.1.90

See server.close() in the node:http module.

M server.closeAllConnections()

Added in: v18.2.0

See server.closeAllConnections() in the node:http module.

M server.closeIdleConnections()

Added in: v18.2.0

See server.closeIdleConnections() in the node:http module.

M server.headersTimeout

Added in: v11.3.0

See server.headersTimeout in the node:http module.

M server.listen()

Starts the HTTPS server listening for encrypted connections. This method is identical to server.listen() from net.Server.

M server.maxHeadersCount

See server.maxHeadersCount in the node:http module.

M server.requestTimeout

Added in: v14.11.0

See server.requestTimeout in the node:http module.

M server.setTimeout([msecs][, callback])

Added in: v0.11.2

See server.setTimeout() in the node:http module.

M server.timeout

历史
版本更改
v13.0.0The default timeout changed from 120s to 0 (no timeout).
v0.11.2Added in: v0.11.2
  • number Default: 0 (no timeout)

See server.timeout in the node:http module.

M server.keepAliveTimeout

Added in: v8.0.0

  • number Default: 5000 (5 seconds)

See server.keepAliveTimeout in the node:http module.

M https.createServer([options][, requestListener])

Added in: v0.3.4

JS

Or

JS

M https.get(options[, callback])

M https.get(url[, options][, callback])

历史
版本更改
v10.9.0The `url` parameter can now be passed along with a separate `options` object.
v7.5.0The `options` parameter can be a WHATWG `URL` object.
v0.3.6Added in: v0.3.6

Like http.get() but for HTTPS.

options can be an object, a string, or a URL object. If options 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.

JS

M https.globalAgent

历史
版本更改
v19.0.0The agent now uses HTTP Keep-Alive by default.
v0.5.9Added in: v0.5.9

Global instance of https.Agent for all HTTPS client requests.

M https.request(options[, callback])

M https.request(url[, options][, callback])

历史
版本更改
v16.7.0, v14.18.0When using a `URL` object parsed username and password will now be properly URI decoded.
v14.1.0, v13.14.0The `highWaterMark` option is accepted now.
v10.9.0The `url` parameter can now be passed along with a separate `options` object.
v9.3.0The `options` parameter can now include `clientCertEngine`.
v7.5.0The `options` parameter can be a WHATWG `URL` object.
v0.3.6Added in: v0.3.6

Makes a request to a secure web server.

The following additional options from tls.connect() are also accepted: ca, cert, ciphers, clientCertEngine, crl, dhparam, ecdhCurve, honorCipherOrder, key, passphrase, pfx, rejectUnauthorized, secureOptions, secureProtocol, servername, sessionIdContext, highWaterMark.

options can be an object, a string, or a URL object. If options 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.

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

JS

Example using options from tls.connect():

JS

Alternatively, opt out of connection pooling by not using an Agent.

JS

Example using a URL as options:

JS

Example pinning on certificate fingerprint, or the public key (similar to pin-sha256):

JS

Outputs for example:

TEXT