Transport modes

Choose one listener plan.

node:httpsBuilt-in TLS server
0Runtime dependencies
CJS + ESMHTTPS configuration
1 pipelineShared static behavior
ModeConfigurationListenersUse it for
HTTPDefault configurationserver.serverCLI tasks, local assets, trusted networks, or TLS at an edge.
HTTPS onlyKey + certificate + https.only:trueserver.secureServerDirect encrypted local or network serving.
HTTP + HTTPSKey + certificate + https.only:falseserver.server and server.secureServerPaired listener deployments and controlled migration.

Both transports use the same roots, Host routing, hooks, request limits, timeouts, range handling, validators, compression, logging, and shutdown contract.

Interface choice: the CLI starts an HTTP listener. CommonJS and ESM carry certificate paths and HTTPS listener modes.

HTTPS-only · ESM

Start one encrypted listener.

ESM · HTTPS-only
import {Server} from 'node-http-server';

const server=new Server({
    root:'./public',
    host:'127.0.0.1',
    https:{
        privateKey:'./certs/private/server.key',
        certificate:'./certs/server.crt',
        port:8443,
        only:true
    }
});

server.deploy((instance,listener)=>{
    console.log('HTTPS ready',listener.address());
});

server.secureServer.once('error',error=>{
    console.error(error);
});
ResultValue
URLhttps://127.0.0.1:8443
HTTP listenerserver.server === null
HTTPS listenerserver.secureServer
address()HTTPS address

HTTP + HTTPS · CommonJS

Run paired listeners from one instance.

CommonJS · paired listeners
const {Server}=require('node-http-server');

const server=new Server({
    root:'./public',
    host:'127.0.0.1',
    port:8080,
    https:{
        privateKey:'./certs/private/server.key',
        certificate:'./certs/server.crt',
        port:8443,
        only:false
    }
});

server.deploy((instance,listener)=>{
    const protocol=listener===instance.secureServer ? 'https' : 'http';
    console.log(protocol+' ready',listener.address());
});

server.server.once('error',error=>console.error(error));
server.secureServer.once('error',error=>console.error(error));
Readiness: the deploy callback runs once for the HTTP listener and once for the HTTPS listener. The listener argument identifies which address became ready.

TLS configuration

Pass filesystem paths to trusted material.

KeyDefaultContract
https.privateKey''Required private-key path when HTTPS is enabled.
https.certificate''Required certificate path when HTTPS is enabled.
https.ca''Optional CA certificate path.
https.passphrasefalseOptional private-key passphrase.
https.port443HTTPS port; 0 requests an OS-selected free port.
https.onlyfalsetrue selects HTTPS-only; false keeps HTTP and HTTPS.

Deployment reads the configured files and constructs the Node HTTPS server before listeners open. Missing, incomplete, unreadable, or invalid material fails startup as one atomic operation.

Listener lifecycle

Inspect and close the listeners you own.

MemberHTTPS-onlyHTTP + HTTPS
server.servernullActive Node HTTP server
server.secureServerActive Node HTTPS serverActive Node HTTPS server
server.address()HTTPS addressHTTP address
server.secureServer.address()HTTPS addressHTTPS address
await server.close()Closes HTTPSCloses both listeners

Listener bind failures use Node's native error event. Attach handlers to every active listener. Repeated and concurrent close() calls share the same close operation.

Certificate operations

Use material issued for the deployment.

DeploymentCertificate sourceOperator action
Local developmentLocal CA or development certificate toolingTrust the CA deliberately and match the requested hostname.
Direct production listenerMaintained public or private CAProtect key files, monitor expiry, and redeploy after rotation.
Reverse proxy or hosting edgeEdge-managed certificateTerminate TLS at the edge and run the server on the protected upstream network.
Private keys: store them outside every served root with narrow filesystem permissions. Generate fresh material for each environment.

Troubleshooting

Map the first signal to the next check.

SignalMeaningNext check
ERR_HTTPS_CONFIGURATIONKey/certificate pair is incomplete.Set both paths and confirm https.only.
Filesystem or PEM error during deployMaterial is unreadable or invalid.Paths, permissions, file format, and passphrase.
EACCES on port 443Process lacks bind permission.Use an allowed port such as 8443 or grant the deployment's intended capability.
Browser certificate warningTrust or hostname validation failed.Certificate chain, SAN hostname, local CA trust, and system clock.
Deploy callback runs twiceBoth listeners became ready.Compare the callback listener with secureServer.
address() shows the HTTP portDual mode prefers the HTTP address.Read secureServer.address() for HTTPS.
CLI output starts with http://The command owns the HTTP interface.Use the CommonJS or ESM configuration above for HTTPS.
Redirect and edge policy: route HTTP-to-HTTPS redirects, certificate automation, SNI, HTTP/2, and advanced TLS policy through application code or a maintained edge.