HTTPS
目录
Added in: v0.10.0
源代码: lib/https.js
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.0 | support `0` `maxCachedSessions` to disable TLS session caching. |
| v2.5.0 | parameter `maxCachedSessions` added to `options` for TLS sessions reuse. |
| v0.4.5 | Added in: v0.4.5 |
An Agent object for HTTPS similar to http.Agent. See
https.request() for more information.
M new Agent([options])
optionsObjectSet of configurable options to set on the agent. Can have the same fields as forhttp.Agent(options), andmaxCachedSessionsnumbermaximum number of TLS cached sessions. Use0to disable TLS session caching. Default:100.servernamestringthe 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 Resumptionfor information about TLS session reuse.
E 'keylog'
Added in: v13.2.0, v12.16.0
lineBufferLine of ASCII text, in NSSSSLKEYLOGFILEformat.tlsSockettls.TLSSocketThetls.TLSSocketinstance 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
- Extends:
tls.Server
See http.Server for more information.
M server.close([callback])
Added in: v0.1.90
callbackFunction- Returns:
https.Server
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
numberDefault:60000
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
numberDefault:2000
See server.maxHeadersCount in the node:http module.
M server.requestTimeout
Added in: v14.11.0
numberDefault:0
See server.requestTimeout in the node:http module.
M server.setTimeout([msecs][, callback])
Added in: v0.11.2
msecsnumberDefault:120000(2 minutes)callbackFunction- Returns:
https.Server
See server.setTimeout() in the node:http module.
M server.timeout
历史
| 版本 | 更改 |
|---|---|
| v13.0.0 | The default timeout changed from 120s to 0 (no timeout). |
| v0.11.2 | Added in: v0.11.2 |
numberDefault: 0 (no timeout)
See server.timeout in the node:http module.
M server.keepAliveTimeout
Added in: v8.0.0
numberDefault:5000(5 seconds)
See server.keepAliveTimeout in the node:http module.
M https.createServer([options][, requestListener])
Added in: v0.3.4
optionsObjectAcceptsoptionsfromtls.createServer(),tls.createSecureContext()andhttp.createServer().requestListenerFunctionA listener to be added to the'request'event.- Returns:
https.Server
JS
Or
JS
M https.get(options[, callback])
M https.get(url[, options][, callback])
历史
| 版本 | 更改 |
|---|---|
| v10.9.0 | The `url` parameter can now be passed along with a separate `options` object. |
| v7.5.0 | The `options` parameter can be a WHATWG `URL` object. |
| v0.3.6 | Added in: v0.3.6 |
urlstring|URLoptionsObject|string|URLAccepts the sameoptionsashttps.request(), with themethodalways set toGET.callbackFunction
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.0 | The agent now uses HTTP Keep-Alive by default. |
| v0.5.9 | Added 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.0 | When using a `URL` object parsed username and password will now be properly URI decoded. |
| v14.1.0, v13.14.0 | The `highWaterMark` option is accepted now. |
| v10.9.0 | The `url` parameter can now be passed along with a separate `options` object. |
| v9.3.0 | The `options` parameter can now include `clientCertEngine`. |
| v7.5.0 | The `options` parameter can be a WHATWG `URL` object. |
| v0.3.6 | Added in: v0.3.6 |
urlstring|URLoptionsObject|string|URLAccepts alloptionsfromhttp.request(), with some differences in default values:protocolDefault:'https:'portDefault:443agentDefault:https.globalAgent
callbackFunction- Returns:
http.ClientRequest
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