HTTP

server.close()

Stops accepting new connections and completes after the server closes. Active requests can delay completion; a production shutdown needs a bounded drain policy. Node 22 also handles idle HTTP keep-alive connections during close. Tested on Node.js 22.16.0. Example output (one line per entry): ["true", "false"].

Formal Syntax & Structure

nodejs syntaxnodejs
server.close([callback])

Parameters & Attributes

NameTypeDescription
callbackFunctionCalled when closing completes, or with an error.

Return Value

http.Server

The server instance.

Working Example

Example Snippetnodejs
import http from 'node:http';const s = http.createServer((q, r) => r.end('ok'));await new Promise(r => s.listen(0, '127.0.0.1', r));console.log(s.listening);await new Promise((resolve, reject) => s.close(e => e ? reject(e) : resolve()));console.log(s.listening);
Node.js Reference