M
tls.createServer
History
Added in: v0.3.2
v0.3.2
Introduced in: v0.10.0
v0.10.0
The
clientCertEngine option depends on custom engine support in OpenSSL which is deprecated in OpenSSL 3.v22.4.0, v20.16.0
The
options parameter can now include ALPNCallback.v20.4.0, v18.19.0
If
ALPNProtocols is set, incoming connections that send an ALPN extension with no supported protocols are terminated with a fatal no_application_protocol alert.v19.0.0
The
options parameter now supports net.createServer() options.v12.3.0
The
options parameter can now include clientCertEngine.v9.3.0
The
ALPNProtocols option can be a TypedArray or DataView now.v8.0.0
ALPN options are supported now.
v5.0.0
tls.createServer(options?, secureConnectionListener?): tls.Server
Attributes
options:
ObjectALPNProtocols:
string[] | Buffer | TypedArray | DataViewAn array of strings,
or a single
Buffer, TypedArray, or DataView containing the supported
ALPN protocols. Buffers should have the format [len][name][len][name]...
e.g. 0x05hello0x05world, where the first byte is the length of the next
protocol name. Passing an array is usually much simpler, e.g.
['hello', 'world']. (Protocols should be ordered by their priority.)ALPNCallback:
FunctionIf set, this will be called when a
client opens a connection using the ALPN extension. One argument will
be passed to the callback: an object containing
servername and
protocols fields, respectively containing the server name from
the SNI extension (if any) and an array of ALPN protocol name strings. The
callback must return either one of the strings listed in
protocols, which will be returned to the client as the selected
ALPN protocol, or undefined, to reject the connection with a fatal alert.
If a string is returned that does not match one of the client's ALPN
protocols, an error will be thrown. This option cannot be used with the
ALPNProtocols option, and setting both options will throw an error.clientCertEngine:
stringName of an OpenSSL engine which can provide the
client certificate. Deprecated.
enableTrace?:
booleanIf
true, tls.TLSSocket.enableTrace() will be
called on new connections. Tracing can be enabled after the secure
connection is established, but this option must be used to trace the secure
connection setup. Default: false.handshakeTimeout?:
numberAbort the connection if the SSL/TLS handshake
does not finish in the specified number of milliseconds.
A
'tlsClientError' is emitted on the tls.Server object whenever
a handshake times out. Default: 120000 (120 seconds).rejectUnauthorized?:
booleanIf not
false the server will reject any
connection which is not authorized with the list of supplied CAs. This
option only has an effect if requestCert is true. Default: true.requestCert?:
booleanIf
true the server will request a certificate from
clients that connect and attempt to verify that certificate. Default:
false.sessionTimeout?:
numberThe number of seconds after which a TLS session
created by the server will no longer be resumable. See
Session Resumption for more information. Default:
300.SNICallback(servername, callback):
FunctionA function that will be
called if the client supports SNI TLS extension. Two arguments will be
passed when called:
servername and callback. callback is an
error-first callback that takes two optional arguments: error and ctx.
ctx, if provided, is a SecureContext instance.
tls.createSecureContext() can be used to get a proper SecureContext.
If callback is called with a falsy ctx argument, the default secure
context of the server will be used. If SNICallback wasn't provided the
default callback with high-level API will be used (see below).ticketKeys:
Buffer48-bytes of cryptographically strong pseudorandom
data. See Session Resumption for more information.
pskCallback:
FunctionFor TLS-PSK negotiation, see Pre-shared keys.
pskIdentityHint:
stringoptional hint to send to a client to help
with selecting the identity during TLS-PSK negotiation. Will be ignored
in TLS 1.3. Upon failing to set pskIdentityHint
'tlsClientError' will be
emitted with 'ERR_TLS_PSK_SET_IDENTITY_HINT_FAILED' code.secureConnectionListener:
FunctionReturns:
tls.ServerCreates a new tls.Server. The secureConnectionListener, if provided, is
automatically set as a listener for the 'secureConnection' event.
The ticketKeys option is automatically shared between node:cluster module
workers.
The following illustrates a simple echo server:
import { createServer } from 'node:tls'; import { readFileSync } from 'node:fs'; const options = { key: readFileSync('server-key.pem'), cert: readFileSync('server-cert.pem'), // This is necessary only if using client certificate authentication. requestCert: true, // This is necessary only if the client uses a self-signed certificate. ca: [ readFileSync('client-cert.pem') ], }; const server = createServer(options, (socket) => { console.log('server connected', socket.authorized ? 'authorized' : 'unauthorized'); socket.write('welcome!\n'); socket.setEncoding('utf8'); socket.pipe(socket); }); server.listen(8000, () => { console.log('server bound'); });
const { createServer } = require('node:tls'); const { readFileSync } = require('node:fs'); const options = { key: readFileSync('server-key.pem'), cert: readFileSync('server-cert.pem'), // This is necessary only if using client certificate authentication. requestCert: true, // This is necessary only if the client uses a self-signed certificate. ca: [ readFileSync('client-cert.pem') ], }; const server = createServer(options, (socket) => { console.log('server connected', socket.authorized ? 'authorized' : 'unauthorized'); socket.write('welcome!\n'); socket.setEncoding('utf8'); socket.pipe(socket); }); server.listen(8000, () => { console.log('server bound'); });
To generate the certificate and key for this example, run:
openssl req -x509 -newkey rsa:2048 -nodes -sha256 -subj '/CN=localhost' \ -keyout server-key.pem -out server-cert.pem
Then, to generate the client-cert.pem certificate for this example, run:
openssl pkcs12 -certpbe AES-256-CBC -export -out client-cert.pem \ -inkey server-key.pem -in server-cert.pem
The server can be tested by connecting to it using the example client from
tls.connect().