Node.js v24.18.1 documentation
- Node.js v24.18.1
-
Table of contents
- HTTPS
-
Index
- Assertion testing
- Asynchronous context tracking
- Async hooks
- Buffer
- C++ addons
- C/C++ addons with Node-API
- C++ embedder API
- Child processes
- Cluster
- Command-line options
- Console
- Crypto
- Debugger
- Deprecated APIs
- Diagnostics Channel
- DNS
- Domain
- Environment Variables
- Errors
- Events
- File system
- Globals
- HTTP
- HTTP/2
- HTTPS
- Inspector
- Internationalization
- Modules: CommonJS modules
- Modules: ECMAScript modules
- Modules:
node:moduleAPI - Modules: Packages
- Modules: TypeScript
- Net
- OS
- Path
- Performance hooks
- Permissions
- Process
- Punycode
- Query strings
- Readline
- REPL
- Report
- Single executable applications
- SQLite
- Stream
- String decoder
- Test runner
- Timers
- TLS/SSL
- Trace events
- TTY
- UDP/datagram
- URL
- Utilities
- V8
- VM
- WASI
- Web Crypto API
- Web Streams API
- Worker threads
- Zlib
- Other versions
- Options
HTTPS#
Source Code: 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:
let https;
try {
https = require('node:https');
} catch (err) {
console.error('https support is disabled!');
}
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:
let https;
try {
https = await import('node:https');
} catch (err) {
console.error('https support is disabled!');
}
Class: https.Agent#
An Agent object for HTTPS similar to http.Agent. See
https.request() for more information.
Like http.Agent, the createConnection(options[, callback]) method can be overridden
to customize how TLS connections are established.
See
agent.createConnection()for details on overriding this method, including asynchronous socket creation with a callback.
new Agent([options])#
options<Object> Set of configurable options to set on the agent. Can have the same fields as forhttp.Agent(options), and-
maxCachedSessions<number> maximum number of TLS cached sessions. Use0to 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 Resumptionfor information about TLS session reuse.
-
Requests that specify a custom checkServerIdentity option are not eligible
for connection reuse or TLS session reuse by an https.Agent, unless the
checkServerIdentity option was specified when constructing the Agent.
Event: 'keylog'#
line<Buffer> Line of ASCII text, in NSSSSLKEYLOGFILEformat.tlsSocket<tls.TLSSocket> Thetls.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:
// ...
https.globalAgent.on('keylog', (line, tlsSocket) => {
fs.appendFileSync('/tmp/ssl-keys.log', line, { mode: 0o600 });
});
Class: https.Server#
- Extends: <tls.Server>
See http.Server for more information.
server.close([callback])#
callback<Function>- Returns: <https.Server>
See server.close() in the node:http module.
server[Symbol.asyncDispose]()#
Calls server.close() and returns a promise that
fulfills when the server has closed.
server.closeAllConnections()#
See server.closeAllConnections() in the node:http module.
server.closeIdleConnections()#
See server.closeIdleConnections() in the node:http module.
server.headersTimeout#
- Type: <number> Default:
60000
See server.headersTimeout in the node:http module.
server.listen()#
Starts the HTTPS server listening for encrypted connections.
This method is identical to server.listen() from net.Server.
server.maxHeadersCount#
- Type: <number> Default:
2000
See server.maxHeadersCount in the node:http module.
server.requestTimeout#
- Type: <number> Default:
300000
See server.requestTimeout in the node:http module.
server.setTimeout([msecs][, callback])#
msecs<number> Default:120000(2 minutes)callback<Function>- Returns: <https.Server>
See server.setTimeout() in the node:http module.
server.keepAliveTimeout#
- Type: <number> Default:
5000(5 seconds)
See server.keepAliveTimeout in the node:http module.
https.createServer([options][, requestListener])#
options<Object> Acceptsoptionsfromtls.createServer(),tls.createSecureContext()andhttp.createServer().requestListener<Function> A listener to be added to the'request'event.- Returns: <https.Server>
// curl -k https://localhost:8000/
import { createServer } from 'node:https';
import { readFileSync } from 'node:fs';
const options = {
key: readFileSync('private-key.pem'),
cert: readFileSync('certificate.pem'),
};
createServer(options, (req, res) => {
res.writeHead(200);
res.end('hello world\n');
}).listen(8000);// curl -k https://localhost:8000/
const https = require('node:https');
const fs = require('node:fs');
const options = {
key: fs.readFileSync('private-key.pem'),
cert: fs.readFileSync('certificate.pem'),
};
https.createServer(options, (req, res) => {
res.writeHead(200);
res.end('hello world\n');
}).listen(8000);