{"id":13403123,"url":"https://github.com/http-party/create-servers","last_synced_at":"2025-03-22T05:08:49.227Z","repository":{"id":13371881,"uuid":"16059633","full_name":"http-party/create-servers","owner":"http-party","description":"Create an http AND/OR an https server and call the same request handler.","archived":false,"fork":false,"pushed_at":"2024-09-16T05:31:17.000Z","size":138,"stargazers_count":45,"open_issues_count":11,"forks_count":17,"subscribers_count":8,"default_branch":"master","last_synced_at":"2024-09-16T06:48:51.933Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/http-party.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2014-01-20T02:58:28.000Z","updated_at":"2024-09-06T11:29:38.000Z","dependencies_parsed_at":"2024-10-25T23:29:13.309Z","dependency_job_id":"2b07eb01-064f-45bd-9e05-2b239e41d38e","html_url":"https://github.com/http-party/create-servers","commit_stats":{"total_commits":76,"total_committers":13,"mean_commits":5.846153846153846,"dds":0.618421052631579,"last_synced_commit":"c07ee4911b423ca809ba03da79f011c187832c9e"},"previous_names":[],"tags_count":24,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/http-party%2Fcreate-servers","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/http-party%2Fcreate-servers/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/http-party%2Fcreate-servers/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/http-party%2Fcreate-servers/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/http-party","download_url":"https://codeload.github.com/http-party/create-servers/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":244757551,"owners_count":20505418,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-07-30T19:01:25.730Z","updated_at":"2025-03-22T05:08:49.198Z","avatar_url":"https://github.com/http-party.png","language":"JavaScript","funding_links":[],"categories":["JavaScript"],"sub_categories":[],"readme":"# create-servers\n\nCreate an http AND/OR an https server and call the same request handler.\n\n## Usage\n\nThe `create-servers` module exports a function that takes a config object and\na node-style callback. The config object must have at minimum an `http` or\n`https` property (or both). The following config properties are supported:\n\n| Property                 | Description                                                                                                                                                                                                                                                                           |\n| ------------------------ | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |\n| `handler`                | Request handler to be used for any server, unless overridden specifically with `http.handler` or `https.handler`.                                                                                                                                                                     |\n| `timeout`                | Socket timeout in milliseconds for any server, unless overridden with `http.timeout` or `https.timeout`. Defaults to the node default of 2 minutes.                                                                                                                                   |\n| `keepAliveTimeout`       | Milliseconds of activity before sockets are destroyed. Defaults to the node default value (currently 5 seconds). |\n| `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.                                                                              |\n| `http.port`              | TCP port for the HTTP server. Defaults to `80`.                                                                                                                                                                                                                                       |\n| `http.host`              | The address the HTTP server is bound to. Defaults to `::` or `0.0.0.0`.                                                                                                                                                                                                               |\n| `http.timeout`           | Socket timeout in milliseconds for the server. If unspecified, the top-level `timeout` configuration is used.                                                                                                                                                                         |\n| `http.keepAliveTimeout`  | Overrides the top-level keepAliveTimeout setting if specified. |\n| `http.handler`           | Handler for HTTP requests. If you want to share a handler with all servers, use a top-level `handler` config property instead.                                                                                                                                                        |\n| `https`                  | Optional object. If present, an HTTPS server is started. You may start multiple HTTPS servers by passing an array of objects                                                                                                                                                          |\n| `https.port`             | TCP port for the HTTPS server. Defaults to `443`.                                                                                                                                                                                                                                     |\n| `https.host`             | The address the HTTPS server is bound to. Defaults to `::` or `0.0.0.0`.                                                                                                                                                                                                              |\n| `https.timeout`          | Socket timeout in milliseconds for the server. If unspecified, the top-level `timeout` configuration is used.                                                                                                                                                                         |\n| `https.keepAliveTimeout`  | Overrides the top-level keepAliveTimeout setting if specified. |\n| `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.                                                                                                     |\n| `https.honorCipherOrder` | If true, prefer the server's specified cipher order instead of the client's. Defaults to `false`.                                                                                                                                                                                     |\n| `https.root`             | Root directory for certificate/key files. See [Certificate normalization](#certificate-normalization) for more details.                                                                                                                                                               |\n| `https.key`              | PEM/file path for the server's private key. See [Certificate normalization](#certificate-normalization) for more details.                                                                                                                                                             |\n| `https.cert`             | PEM/file path(s) for the server's certificate. See [Certificate normalization](#certificate-normalization) for more details.                                                                                                                                                          |\n| `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. |\n| `https.sni`              | See [SNI Support](#sni-support).                                                                                                                                                                                                                                                      |\n| `https.handler`          | Handler for HTTPS requests. If you want to share a handler with all servers, use a top-level `handler` config property instead.                                                                                                                                                       |\n| `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.   |\n| `http2`                  | Optional object. If present, an HTTP/2 server is started. You may start multiple HTTP/2 servers by passing an array of objects                                                                                                                                                        |\n| `http2.allowHTTP1`       | Enable [ALPN negotiation] allowing support for both HTTPS and HTTP/2 on the same socket.                                                                                                                                                                                              |\n| `http2.*`                | The same `https` security options are allowed, as well as any other properties supported by [http2.createSecureServer](https://nodejs.org/dist/latest-v8.x/docs/api/http2.html#http2_http2_createsecureserver_options_onrequesthandler).                                              |\n\nIf successful, the `create-servers` callback is passed an object with the\nfollowing properties:\n\n| Property | Description                                                                                     |\n| -------- | ----------------------------------------------------------------------------------------------- |\n| `http`   | The HTTP server that was created, if any. If creating multiple servers, this will be an Array.  |\n| `https`  | The HTTPS server that was created, if any. If creating multiple servers, this will be an Array. |\n\n### Certificate Normalization\n\n`create-servers` provides some conveniences for `https.ca`, `https.key`, and\n`https.cert` config properties. You may use PEM data directly (inside a `Buffer`\nor string) or a file name. When using a file name, you must also set an\n`https.root` config property if using relative paths to cert/key files.\n\n`https.ca`, `https.cert`, and `https.key` also support specifying an Array.\nGiven an array for `cert`, you must have a matching array for `key` so each cert\ncan be matched with its private key.\n\n```js\nconst createServers = require('create-servers');\n\ncreateServers({\n  https: {\n    root: '/cert/path',\n    cert: ['cert1.crt', 'cert2.crt'],\n    key: ['cert1.key', 'cert2.key']\n  }\n}, err =\u003e {\n  // ...\n})\n```\n\nIf you have a cert that is signed by an intermediate CA, your server will need\nto append the untrusted parts of the CA chain with your cert. To make this more\nconvenient, `create-servers` lets you use an array to automatically create a\nchain.\n\n```js\nconst createServers = require('create-servers');\n\ncreateServers({\n  https: {\n    root: '/cert/path',\n    cert: ['cert.crt', 'intermediate.crt'],\n    key: 'cert.key'\n  }\n}, err =\u003e {\n  // ...\n})\n```\n\nIf you are specifying multiple certs _and_ you want to create chains for each,\nuse an array of arrays.\n\n```js\nconst createServers = require('create-servers');\n\ncreateServers({\n  https: {\n    root: '/cert/path',\n    cert: [['cert1.crt', 'intermediate.crt'], 'cert2.crt'],\n    key: ['cert1.key', 'cert2.key']\n  }\n}, err =\u003e {\n  // ...\n})\n```\n\n### SNI Support\n\n[Server Name Indication](https://en.wikipedia.org/wiki/Server_Name_Indication),\nor SNI, lets HTTPS clients announce which hostname they wish to connect to\nbefore the server sends its certificate, enabling the use of the same server for\nmultiple hosts. Although `SNICallback` can be used to support this, you lose the\nconvenient certificate normalization provided by `create-servers`. The `sni`\nconfig option provides an easier way.\n\nThe `sni` option is an object with each key being a supported hostname and each\nvalue being a subset of the HTTPS settings listed above. HTTPS settings defined\nat the top level are used as defaults for the hostname-specific settings.\n\n```js\nconst createServers = require('create-servers');\n\ncreateServers(\n  {\n    https: {\n      port: 443,\n      sni: {\n        'example1.com': {\n          key: '/certs/private/example1.com.key',\n          cert: '/certs/public/example1.com.crt'\n        },\n        'example2.com': {\n          key: '/certs/private/example2.com.key',\n          cert: '/certs/public/example2.com.crt'\n        }\n      }\n    },\n    handler: function (req, res) {\n      res.end('Hello');\n    }\n  },\n  function (errs) {\n    if (errs) {\n      return console.log(errs.https);\n    }\n\n    console.log('Listening on 443');\n  }\n);\n```\n\nUse `*` in the hostname for wildcard certs. Example: `*.example.com`. The\nfollowing settings are supported in the host-specific configuration:\n\n* key\n* cert\n* ca\n* ciphers\n* honorCipherOrder\n* Anything else supported by [`tls.createSecureContext`](https://nodejs.org/dist/latest-v8.x/docs/api/tls.html#tls_tls_createsecurecontext_options)\n\n## NOTE on Security\nInspired by [`iojs`][iojs] and a well written [article][article], we have defaulted\nour [ciphers][ciphers] to support \"perfect-forward-security\" as well as removing insecure\ncipher suites from being a possible choice. With this in mind,\nbe aware that we will no longer support ie6 on windows XP by default.\n\n## Examples\n\n### http\n\n```js\nvar createServers = require('create-servers');\n\nvar servers = createServers(\n  {\n    http: 80,\n    handler: function (req, res) {\n      res.end('http only');\n    }\n  },\n  function (errs) {\n    if (errs) {\n      return console.log(errs.http);\n    }\n\n    console.log('Listening on 80');\n  }\n);\n```\n\n### https\n\n```js\nvar servers = createServers(\n  {\n    https: {\n      port: 443,\n      root: '/path/to/ssl/files',\n      key: 'your-key.pem',\n      cert: 'your-cert.pem',\n      ca: 'your-ca.pem' // Can be an Array of CAs\n    },\n    handler: function (req, res) {\n      res.end('https only');\n    }\n  },\n  function (errs) {\n    if (errs) {\n      return console.log(errs.https);\n    }\n\n    console.log('Listening on 443');\n  }\n);\n```\n\n### http \u0026\u0026 https\n\n```js\nvar servers = createServers(\n  {\n    http: 80,\n    https: {\n      port: 443,\n      root: '/path/to/ssl/files',\n      key: 'your-key.pem',\n      cert: 'your-cert.pem',\n      ca: 'your-ca.pem' // Can be an Array of CAs\n    },\n    handler: function (req, res) {\n      res.end('http AND https');\n    }\n  },\n  function (errs, servers) {\n    if (errs) {\n      return Object.keys(errs).forEach(function (key) {\n        console.log('Error ' + key + ': ' + errs[key]);\n        if (servers[key]) {\n          servers[key].close();\n        }\n      });\n    }\n\n    console.log('Listening on 80 and 443');\n  }\n);\n```\n\n### http \u0026\u0026 https (different handlers)\n\n```js\nvar servers = createServers(\n  {\n    http: {\n      port: 80,\n      handler: function (req, res) {\n        res.end('http');\n      }\n    },\n    https: {\n      port: 443,\n      root: '/path/to/ssl/files',\n      key: 'your-key.pem',\n      cert: 'your-cert.pem',\n      ca: 'your-ca.pem', // Can be an Array of CAs\n      handler: function (req, res) {\n        res.end('https');\n      }\n    }\n  },\n  function (errs, servers) {\n    if (errs) {\n      return Object.keys(errs).forEach(function (key) {\n        console.log('Error ' + key + ': ' + errs[key]);\n        if (servers[key]) {\n          servers[key].close();\n        }\n      });\n    }\n\n    console.log('Listening on 80 and 443');\n  }\n);\n```\n\n## Author: [Charlie Robbins](https://github.com/indexzero)\n## License: MIT\n\n[article]: https://certsimple.com/blog/a-plus-node-js-ssl\n[iojs]: https://github.com/iojs/io.js\n[ciphers]: https://iojs.org/api/tls.html#tls_tls_createserver_options_secureconnectionlistener\n[ALPN negotiation]: https://nodejs.org/api/http2.html#http2_alpn_negotiation\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhttp-party%2Fcreate-servers","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fhttp-party%2Fcreate-servers","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhttp-party%2Fcreate-servers/lists"}