On this page

    tls.createServer(options?, secureConnectionListener?): tls.Server
    Attributes
    options:Object
    ALPNProtocols:string[] | Buffer | TypedArray | DataView
    An 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:Function
    If 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:string
    Name of an OpenSSL engine which can provide the client certificate. Deprecated.
    enableTrace?:boolean
    If 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?:number
    Abort 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?:boolean
    If 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?:boolean
    If true the server will request a certificate from clients that connect and attempt to verify that certificate. Default: false.
    sessionTimeout?:number
    The 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):Function
    A 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:Buffer
    48-bytes of cryptographically strong pseudorandom data. See Session Resumption for more information.
    pskCallback:Function
    For TLS-PSK negotiation, see Pre-shared keys.
    pskIdentityHint:string
    optional 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:Function
    Returns:tls.Server

    Creates 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().