From 938008622c2e30a1be955b28a3c522fc303d9401 Mon Sep 17 00:00:00 2001 From: mary marchini Date: Sat, 11 May 2024 11:41:51 -0700 Subject: [PATCH] doc: add examples and notes to http server.close et al Add examples to `http` server.close, server.closeAllConnections, server.closeIdleConnections. Also add notes about usage for both server.close*Connections libraries. PR-URL: https://github.com/nodejs/node/pull/49091 Reviewed-By: Paolo Insogna Reviewed-By: Luigi Pinca Reviewed-By: Marco Ippolito --- doc/api/http.md | 79 ++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 78 insertions(+), 1 deletion(-) diff --git a/doc/api/http.md b/doc/api/http.md index 890f1fdbf665df..5e6ed88aa441d7 100644 --- a/doc/api/http.md +++ b/doc/api/http.md @@ -1679,13 +1679,59 @@ connected to this server which are not sending a request or waiting for a response. See [`net.Server.close()`][]. +```js +const http = require('node:http'); + +const server = http.createServer({ keepAliveTimeout: 60000 }, (req, res) => { + res.writeHead(200, { 'Content-Type': 'application/json' }); + res.end(JSON.stringify({ + data: 'Hello World!', + })); +}); + +server.listen(8000); +// Close the server after 10 seconds +setTimeout(() => { + server.close(() => { + console.log('server on port 8000 closed successfully'); + }); +}, 10000); +``` + ### `server.closeAllConnections()` -Closes all connections connected to this server. +Closes all connections connected to this server, including active connections +connected to this server which are sending a request or waiting for a response. + +> This is a forceful way of closing all connections and should be used with +> caution. Whenever using this in conjunction with `server.close`, calling this +> _after_ `server.close` is recommended as to avoid race conditions where new +> connections are created between a call to this and a call to `server.close`. + +```js +const http = require('node:http'); + +const server = http.createServer({ keepAliveTimeout: 60000 }, (req, res) => { + res.writeHead(200, { 'Content-Type': 'application/json' }); + res.end(JSON.stringify({ + data: 'Hello World!', + })); +}); + +server.listen(8000); +// Close the server after 10 seconds +setTimeout(() => { + server.close(() => { + console.log('server on port 8000 closed successfully'); + }); + // Closes all connections, ensuring the server closes successfully + server.closeAllConnections(); +}, 10000); +``` ### `server.closeIdleConnections()` @@ -1696,6 +1742,37 @@ added: v18.2.0 Closes all connections connected to this server which are not sending a request or waiting for a response. +> Starting with Node.js 19.0.0, there's no need for calling this method in +> conjunction with `server.close` to reap `keep-alive` connections. Using it +> won't cause any harm though, and it can be useful to ensure backwards +> compatibility for libraries and applications that need to support versions +> older than 19.0.0. Whenever using this in conjunction with `server.close`, +> calling this _after_ `server.close` is recommended as to avoid race +> conditions where new connections are created between a call to this and a +> call to `server.close`. + +```js +const http = require('node:http'); + +const server = http.createServer({ keepAliveTimeout: 60000 }, (req, res) => { + res.writeHead(200, { 'Content-Type': 'application/json' }); + res.end(JSON.stringify({ + data: 'Hello World!', + })); +}); + +server.listen(8000); +// Close the server after 10 seconds +setTimeout(() => { + server.close(() => { + console.log('server on port 8000 closed successfully'); + }); + // Closes idle connections, such as keep-alive connections. Server will close + // once remaining active connections are terminated + server.closeIdleConnections(); +}, 10000); +``` + ### `server.headersTimeout`