I've found my self writing this particular piece of snippet over and over again.
If you need to have a common API for creating a HTTP, HTTPS or SPDY server this
might be the module that you've been waiting for.
Add it to your Node.js project by running
npm install --save create-server
Creating a server
In all code examples we assume that you've required the module and saved it as
var create = require('create-server');
create variable is now a function which can be used to create different
types of servers. The function accepts 2 different arguments:
- A number which should be the port number or object with the configuration for
- Optionally, an object with different callback methods.
The following properties can be configured:
- port: The port number we should listen on. Also used to determine which
type of server we need to create.
- hostname: What interface we should listen on.
- spdy: Create SPDY server instead of a HTTPS server.
- root: The root folder that contains your HTTPS certs.
- key, cert, ca, pfx, crl Path or array of paths which will be read out the
correct files. The path should be relative to the root option.
- redirect: Start up an optional HTTP server who will redirect users to the
port you're listening on. The supplied value should be the port number we need
to listen on.
- listen: Do need to start listening to the server for you?
The following properties can be provided as callback object:
- close: Server is closed.
- request: Received a new incoming request.
- upgrade: Received a HTTP upgrade request.
- listening: We're now listening. Receives an optional error as first
- error: Received a new error on the server.
- https: A new HTTPS server has been created.
- http: A new HTTP server has been created.
- spdy: A new SPDY server has been created.
When creating a secure server, we will do our best to provide sane defaults that
will protect your server against known secure server attacks such as POODLE, we
also update the cipher list to prevent attacks such as heart bleed. This can be
overridden by supplying your own
keys as option.