@@ -1679,13 +1679,59 @@ connected to this server which are not sending a request or waiting for
1679
1679
a response.
1680
1680
See [ ` net.Server.close() ` ] [ ] .
1681
1681
1682
+ ``` js
1683
+ const http = require (' node:http' );
1684
+
1685
+ const server = http .createServer ({ keepAliveTimeout: 60000 }, (req , res ) => {
1686
+ res .writeHead (200 , { ' Content-Type' : ' application/json' });
1687
+ res .end (JSON .stringify ({
1688
+ data: ' Hello World!' ,
1689
+ }));
1690
+ });
1691
+
1692
+ server .listen (8000 );
1693
+ // Close the server after 10 seconds
1694
+ setTimeout (() => {
1695
+ server .close (() => {
1696
+ console .log (' server on port 8000 closed successfully' );
1697
+ });
1698
+ }, 10000 );
1699
+ ```
1700
+
1682
1701
### ` server.closeAllConnections() `
1683
1702
1684
1703
<!-- YAML
1685
1704
added: v18.2.0
1686
1705
-->
1687
1706
1688
- Closes all connections connected to this server.
1707
+ Closes all connections connected to this server, including active connections
1708
+ connected to this server which are sending a request or waiting for a response.
1709
+
1710
+ > This is a forceful way of closing all connections and should be used with
1711
+ > caution. Whenever using this in conjunction with ` server.close ` , calling this
1712
+ > _ after_ ` server.close ` is recommended as to avoid race conditions where new
1713
+ > connections are created between a call to this and a call to ` server.close ` .
1714
+
1715
+ ``` js
1716
+ const http = require (' node:http' );
1717
+
1718
+ const server = http .createServer ({ keepAliveTimeout: 60000 }, (req , res ) => {
1719
+ res .writeHead (200 , { ' Content-Type' : ' application/json' });
1720
+ res .end (JSON .stringify ({
1721
+ data: ' Hello World!' ,
1722
+ }));
1723
+ });
1724
+
1725
+ server .listen (8000 );
1726
+ // Close the server after 10 seconds
1727
+ setTimeout (() => {
1728
+ server .close (() => {
1729
+ console .log (' server on port 8000 closed successfully' );
1730
+ });
1731
+ // Closes all connections, ensuring the server closes successfully
1732
+ server .closeAllConnections ();
1733
+ }, 10000 );
1734
+ ```
1689
1735
1690
1736
### ` server.closeIdleConnections() `
1691
1737
@@ -1696,6 +1742,37 @@ added: v18.2.0
1696
1742
Closes all connections connected to this server which are not sending a request
1697
1743
or waiting for a response.
1698
1744
1745
+ > Starting with Node.js 19.0.0, there's no need for calling this method in
1746
+ > conjunction with ` server.close ` to reap ` keep-alive ` connections. Using it
1747
+ > won't cause any harm though, and it can be useful to ensure backwards
1748
+ > compatibility for libraries and applications that need to support versions
1749
+ > older than 19.0.0. Whenever using this in conjunction with ` server.close ` ,
1750
+ > calling this _ after_ ` server.close ` is recommended as to avoid race
1751
+ > conditions where new connections are created between a call to this and a
1752
+ > call to ` server.close ` .
1753
+
1754
+ ``` js
1755
+ const http = require (' node:http' );
1756
+
1757
+ const server = http .createServer ({ keepAliveTimeout: 60000 }, (req , res ) => {
1758
+ res .writeHead (200 , { ' Content-Type' : ' application/json' });
1759
+ res .end (JSON .stringify ({
1760
+ data: ' Hello World!' ,
1761
+ }));
1762
+ });
1763
+
1764
+ server .listen (8000 );
1765
+ // Close the server after 10 seconds
1766
+ setTimeout (() => {
1767
+ server .close (() => {
1768
+ console .log (' server on port 8000 closed successfully' );
1769
+ });
1770
+ // Closes idle connections, such as keep-alive connections. Server will close
1771
+ // once remaining active connections are terminated
1772
+ server .closeIdleConnections ();
1773
+ }, 10000 );
1774
+ ```
1775
+
1699
1776
### ` server.headersTimeout `
1700
1777
1701
1778
<!-- YAML
0 commit comments