<commit_before># server-terminate

![TypeScript definition](https://img.shields.io/badge/TypeScript%20Definition-%E2%9C%93-blue.svg)

Allow terminating an HTTP server in an orderly fashion:
* Immediately closes keep-alive connections that are not being used by any HTTP request. 
* Waits for running HTTP requests to finish before closing their connections.
* Closes connections with running HTTP requests after a given timeout.

If you want to destroy all open connections without waiting for HTTP requests to finish,
use the module [server-destroy](https://github.com/isaacs/server-destroy).

## Installation
```sh
npm install server-terminate
```

## Usage
```javascript
var enableTerminate = require('server-terminate');
var http = require('http');

var server = http.createServer(function onRequest(req, res) {
  // Do your stuff here
});
enableTerminate(server).listen(PORT));


// When you want to stop your server...
server.terminate(function(err, terminatedByTimeout) {
  // You get here when all connections have been closed
});
```

Or if you are using TypeScript:
```typescript
import * as http from 'http';
import enableTerminate from 'server-terminate';

let server: http.Server = http.createServer(function onRequest(req: http.ServerRequest, res: http.ServerResponse) {
    // Do your stuff here
});
enableTerminate(server).listen(PORT));

// When you want to stop your server...
server.terminate((err, terminatedByTimeout) => {
    // You get here when all connections have been closed
});
```

You can set a timeout to force connection closing even when they are still being used by running HTTP requests.
It is measured in milliseconds and defaults to 30000.
```javascript
enableTerminate(server, {timeout: 10000}).listen(PORT));
```

If the server terminates by timeout the second parameter of the callback will be `true`.

## License
MIT<commit_msg>[[DOCS]] Fix examples in the README<commit_after># server-terminate

![TypeScript definition](https://img.shields.io/badge/TypeScript%20Definition-%E2%9C%93-blue.svg)

Allow terminating an HTTP server in an orderly fashion:
* Immediately closes keep-alive connections that are not being used by any HTTP request. 
* Waits for running HTTP requests to finish before closing their connections.
* Closes connections with running HTTP requests after a given timeout.

If you want to destroy all open connections without waiting for HTTP requests to finish,
use the module [server-destroy](https://github.com/isaacs/server-destroy).

## Installation
```sh
npm install server-terminate
```

## Usage
```javascript
var enableTerminate = require('server-terminate');
var http = require('http');

var server = http.createServer(function onRequest(req, res) {
  // Do your stuff here
});
enableTerminate(server).listen(PORT);


// When you want to stop your server...
server.terminate(function(err, terminatedByTimeout) {
  // You get here when all connections have been closed
});
```

Or if you are using TypeScript:
```typescript
import * as http from 'http';
import enableTerminate from 'server-terminate';

let server: http.Server = http.createServer(function onRequest(req: http.ServerRequest, res: http.ServerResponse) {
    // Do your stuff here
});
enableTerminate(server).listen(PORT);

// When you want to stop your server...
server.terminate((err, terminatedByTimeout) => {
    // You get here when all connections have been closed
});
```

You can set a timeout to force connection closing even when they are still being used by running HTTP requests.
It is measured in milliseconds and defaults to 30000.
```javascript
enableTerminate(server, {timeout: 10000}).listen(PORT);
```

If the server terminates by timeout the second parameter of the callback will be `true`.

## License
MIT
{'fraction_non_alphanumeric': 0.10300771892467395, 'fraction_numerical': 0.008517434122970456, 'mean_word_length': 4.890282131661442, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 4, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4055407', 'n_tokens_mistral': 1048, 'n_tokens_neox': 985, 'n_words': 412}