Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit e0148e2

Browse filesBrowse files
mmarchinitargos
authored andcommitted
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: #49091 Reviewed-By: Paolo Insogna <paolo@cowtech.it> Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Marco Ippolito <marcoippolito54@gmail.com>
1 parent 030f56e commit e0148e2
Copy full SHA for e0148e2

File tree

Expand file treeCollapse file tree

1 file changed

+78
-1
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

1 file changed

+78
-1
lines changed
Open diff view settings
Collapse file

‎doc/api/http.md‎

Copy file name to clipboardExpand all lines: doc/api/http.md
+78-1Lines changed: 78 additions & 1 deletion
  • Display the source diff
  • Display the rich diff
Original file line numberDiff line numberDiff line change
@@ -1679,13 +1679,59 @@ connected to this server which are not sending a request or waiting for
16791679
a response.
16801680
See [`net.Server.close()`][].
16811681

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+
16821701
### `server.closeAllConnections()`
16831702

16841703
<!-- YAML
16851704
added: v18.2.0
16861705
-->
16871706

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+
```
16891735

16901736
### `server.closeIdleConnections()`
16911737

@@ -1696,6 +1742,37 @@ added: v18.2.0
16961742
Closes all connections connected to this server which are not sending a request
16971743
or waiting for a response.
16981744

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+
16991776
### `server.headersTimeout`
17001777

17011778
<!-- YAML

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.