Skip to content

Commit 7eaae28

Browse files
jpage-godaddyindexzero
authored andcommitted
Support creating multiple HTTP or HTTPS servers (#21)
* Support creating multiple HTTP or HTTPS servers * Add support for multiple http(s) servers * Fix test expecting error that was not failing
1 parent e4bf545 commit 7eaae28

File tree

4 files changed

+327
-191
lines changed

4 files changed

+327
-191
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,4 @@ npm-debug.log
1515
node_modules
1616

1717
.idea
18+
.vscode

README.md

Lines changed: 39 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -5,45 +5,45 @@ Create an http AND/OR an https server and call the same request handler.
55
## Usage
66

77
The `create-servers` module exports a function that takes a config object and
8-
a node-style callback. The config object must have at minimum an `http` or
8+
a node-style callback. The config object must have at minimum an `http` or
99
`https` property (or both). The following config properties are supported:
1010

11-
| Property | Description |
12-
|---------------------------|-------------|
13-
| `handler` | Request handler to be used for any server, unless overridden specifically with `http.handler` or `https.handler`. |
14-
| `timeout` | Socket timeout in milliseconds for any server, unless overridden with `http.timeout` or `https.timeout`. Defaults to the node default of 2 minutes. |
15-
| `http` | Optional. If present, an HTTP server is started. This can be an object or a number. If it's a number, it's used as the TCP port for an HTTP server. |
16-
| `http.port` | TCP port for the HTTP server. Defaults to `80`. |
17-
| `http.host` | The address the HTTP server is bound to. Defaults to `::` or `0.0.0.0`. |
18-
| `http.timeout` | Socket timeout in milliseconds for the server. If unspecified, the top-level `timeout` configuration is used. |
19-
| `http.handler` | Handler for HTTP requests. If you want to share a handler with all servers, use a top-level `handler` config property instead. |
20-
| `https` | Optional object. If present, an HTTPS server is started. |
21-
| `https.port` | TCP port for the HTTPS server. Defaults to `443`. |
22-
| `https.host` | The address the HTTPS server is bound to. Defaults to `::` or `0.0.0.0`. |
23-
| `https.timeout` | Socket timeout in milliseconds for the server. If unspecified, the top-level `timeout` configuration is used. |
24-
| `https.ciphers` | Defaults to a [default cipher suite](#note-on-security). To customize, either supply a colon-separated string or array of strings for the ciphers you want the server to support. |
25-
| `https.honorCipherOrder` | If true, prefer the server's specified cipher order instead of the client's. Defaults to `false`. |
26-
| `https.root` | Root directory for certificate/key files. See [Certificate normalization](#certificate-normalization) for more details. |
27-
| `https.key` | PEM/file path for the server's private key. See [Certificate normalization](#certificate-normalization) for more details. |
28-
| `https.cert` | PEM/file path(s) for the server's certificate. See [Certificate normalization](#certificate-normalization) for more details. |
29-
| `https.ca` | Cert or array of certs specifying trusted authorities for peer certificates. Only required if your server accepts client certificate connections signed by authorities that are not trusted by default. See [Certificate normalization](#certificate-normalization) for more details. |
30-
| `https.sni` | See [SNI Support](#sni-support). |
31-
| `https.handler` | Handler for HTTPS requests. If you want to share a handler with all servers, use a top-level `handler` config property instead. |
32-
| `https.*` | Any other properties supported by [https.createServer](https://nodejs.org/dist/latest-v8.x/docs/api/https.html#https_https_createserver_options_requestlistener) can be added to the https object, except `secureProtocol` and `secureOptions` which are set to recommended values. |
11+
| Property | Description |
12+
| ------------------------ | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
13+
| `handler` | Request handler to be used for any server, unless overridden specifically with `http.handler` or `https.handler`. |
14+
| `timeout` | Socket timeout in milliseconds for any server, unless overridden with `http.timeout` or `https.timeout`. Defaults to the node default of 2 minutes. |
15+
| `http` | Optional. If present, an HTTP server is started. This can be an object or a number. If it's a number, it's used as the TCP port for an HTTP server. You may also use an Array to start multiple servers. |
16+
| `http.port` | TCP port for the HTTP server. Defaults to `80`. |
17+
| `http.host` | The address the HTTP server is bound to. Defaults to `::` or `0.0.0.0`. |
18+
| `http.timeout` | Socket timeout in milliseconds for the server. If unspecified, the top-level `timeout` configuration is used. |
19+
| `http.handler` | Handler for HTTP requests. If you want to share a handler with all servers, use a top-level `handler` config property instead. |
20+
| `https` | Optional object. If present, an HTTPS server is started. You may start multiple HTTPS servers by passing an array of objects |
21+
| `https.port` | TCP port for the HTTPS server. Defaults to `443`. |
22+
| `https.host` | The address the HTTPS server is bound to. Defaults to `::` or `0.0.0.0`. |
23+
| `https.timeout` | Socket timeout in milliseconds for the server. If unspecified, the top-level `timeout` configuration is used. |
24+
| `https.ciphers` | Defaults to a [default cipher suite](#note-on-security). To customize, either supply a colon-separated string or array of strings for the ciphers you want the server to support. |
25+
| `https.honorCipherOrder` | If true, prefer the server's specified cipher order instead of the client's. Defaults to `false`. |
26+
| `https.root` | Root directory for certificate/key files. See [Certificate normalization](#certificate-normalization) for more details. |
27+
| `https.key` | PEM/file path for the server's private key. See [Certificate normalization](#certificate-normalization) for more details. |
28+
| `https.cert` | PEM/file path(s) for the server's certificate. See [Certificate normalization](#certificate-normalization) for more details. |
29+
| `https.ca` | Cert or array of certs specifying trusted authorities for peer certificates. Only required if your server accepts client certificate connections signed by authorities that are not trusted by default. See [Certificate normalization](#certificate-normalization) for more details. |
30+
| `https.sni` | See [SNI Support](#sni-support). |
31+
| `https.handler` | Handler for HTTPS requests. If you want to share a handler with all servers, use a top-level `handler` config property instead. |
32+
| `https.*` | Any other properties supported by [https.createServer](https://nodejs.org/dist/latest-v8.x/docs/api/https.html#https_https_createserver_options_requestlistener) can be added to the https object, except `secureProtocol` and `secureOptions` which are set to recommended values. |
3333

3434
If successful, the `create-servers` callback is passed an object with the
3535
following properties:
3636

37-
| Property | Description |
38-
|----------|-------------|
39-
| `http` | The HTTP server that was created, if any |
40-
| `https` | The HTTPS server that was created, if any |
37+
| Property | Description |
38+
| -------- | ----------------------------------------------------------------------------------------------- |
39+
| `http` | The HTTP server that was created, if any. If creating multiple servers, this will be an Array. |
40+
| `https` | The HTTPS server that was created, if any. If creating multiple servers, this will be an Array. |
4141

4242
### Certificate Normalization
4343

44-
`create-servers` provides some conveniences for `https.ca`, `https.key`, and
44+
`create-servers` provides some conveniences for `https.ca`, `https.key`, and
4545
`https.cert` config properties. You may use PEM data directly (inside a `Buffer`
46-
or string) or a file name. When using a file name, you must also set an
46+
or string) or a file name. When using a file name, you must also set an
4747
`https.root` config property if using relative paths to cert/key files.
4848

4949
`https.ca`, `https.cert`, and `https.key` also support specifying an Array.
@@ -159,12 +159,13 @@ following settings are supported in the host-specific configuration:
159159
Inspired by [`iojs`][iojs] and a well written [article][article], we have defaulted
160160
our [ciphers][ciphers] to support "perfect-forward-security" as well as removing insecure
161161
cipher suites from being a possible choice. With this in mind,
162-
be aware that we will no longer support ie6 on windows XP by default.
162+
be aware that we will no longer support ie6 on windows XP by default.
163163

164164
## Examples
165165

166166
### http
167-
``` js
167+
168+
```js
168169
var createServers = require('create-servers');
169170

170171
var servers = createServers(
@@ -185,7 +186,8 @@ var servers = createServers(
185186
```
186187

187188
### https
188-
``` js
189+
190+
```js
189191
var servers = createServers(
190192
{
191193
https: {
@@ -210,7 +212,8 @@ var servers = createServers(
210212
```
211213

212214
### http && https
213-
``` js
215+
216+
```js
214217
var servers = createServers(
215218
{
216219
http: 80,
@@ -241,7 +244,8 @@ var servers = createServers(
241244
```
242245

243246
### http && https (different handlers)
244-
``` js
247+
248+
```js
245249
var servers = createServers(
246250
{
247251
http: {
@@ -281,4 +285,4 @@ var servers = createServers(
281285

282286
[article]: https://certsimple.com/blog/a-plus-node-js-ssl
283287
[iojs]: https://github.com/iojs/io.js
284-
[ciphers]: https://iojs.org/api/tls.html#tls_tls_createserver_options_secureconnectionlistener
288+
[ciphers]: https://iojs.org/api/tls.html#tls_tls_createserver_options_secureconnectionlistener

0 commit comments

Comments
 (0)