1.0.0 • Published 2 years ago

http-server-plus v1.0.0

Weekly downloads
406
License
ISC
Repository
github
Last release
2 years ago

http-server-plus

Package Version Build Status PackagePhobia Latest Commit

Augmented http.Server, HTTP/HTTPS/HTTP2 and multiple ports on the same instance

Install

Installation of the npm package:

npm install --save http-server-plus

Example

// The `create()` method can also take a `requestListener`, just like
// `http.createServer()`.
var app = require("express")();
var server = require("http-server-plus").create(app);

// The listen method returns a promise which resolves when the server
// starts listening.
require("bluebird")
  .all([
    // Listen on port localhost:80.
    server.listen({
      hostname: "localhost",
    }),

    // Listen on port 443, using HTTPS.
    server.listen({
      cert: require("fs").readFileSync(__dirname + "/certificate.pem"),
      key: require("fs").readFileSync(__dirname + "/private_key.pem"),
    }),

    server.listen({
      // Listen on localhost:8080
      port: 8080,
    })

    // Listen on socket.
    server.listen({
      socket: __dirname + "/http.sock",
    }),

    // Listen on file descriptor (with systemd for instance).
    server.listen({
      fd: 3,
    }),

    // Listen on a socket created by systemd.
    server.listen({
      systemdSocket: 0, // this is a socket index
    }),
  ])
  .then(function (niceAddresses) {
    console.log("server is listening on", niceAddresses);
  })
  .catch(function (error) {
    console.error("The server could not listen on", error.niceAddress);
  });

As a convenience, if hostname is localhost, it will listen on both IPv4 (127.0.0.1) and IPv6 (::1), similar to what Node does if no hostname are provided (0.0.0.0 and ::).

Using ES2016's async functions:

import createExpressApp from "express";
import { create } from "http-server-plus";

async function main() {
  const app = createExpressApp();

  // The `create()` method can also take a `requestListener`, just
  // like `http.createServer()`.
  const server = create(app);

  try {
    // The listen method returns a promise which resolves when the server
    // starts listening.
    const niceAddresses = await Promise.all([
      // Listen on port localhost:80.
      server.listen({
        hostname: "localhost",
        port: 80,
      }),

      // Listen on port 443, using HTTPS.
      server.listen({
        port: 443,

        cert: require("fs").readFileSync(__dirname + "/certificate.pem"),
        key: require("fs").readFileSync(__dirname + "/private_key.pem"),
      }),

      // Listen on socket.
      server.listen({
        socket: __dirname + "/http.sock",
      }),
    ]);

    console.log("the server is listening on", niceAdresses);
  } catch (error) {
    console.error("the server could not listen on", error.niceAddress);
  }
}

Using HTTP/2 for Node >= 8:

var server = require("http-server-plus").create(
  {
    createSecureServer: require("http2").createSecureServer,
  },
  app
);

To enable HTTP/2 for Node < 8, you need to install spdy:

npm install --save spdy

And:

var server = require("http-server-plus").create(
  {
    createSecureServer: require("spdy").createServer,
  },
  app
);

Contributions

Contributions are very welcomed, either on the documentation or on the code.

You may:

  • report any issue you've encountered;
  • fork and create a pull request.

License

ISC © Julien Fontanet

1.0.0

2 years ago

0.12.0

3 years ago

0.11.1

3 years ago

0.11.0

6 years ago

0.10.0

7 years ago

0.9.0

7 years ago

0.8.0

9 years ago

0.7.0

9 years ago

0.6.4

9 years ago

0.6.2

9 years ago

0.6.1

9 years ago

0.5.3

10 years ago

0.5.2

10 years ago

0.5.1

10 years ago

0.5.0

10 years ago

0.4.1

10 years ago

0.4.0

10 years ago

0.3.0

11 years ago

0.2.3

11 years ago

0.2.2

11 years ago

0.2.1

11 years ago

0.2.0

11 years ago

0.1.1

11 years ago

0.1.0

11 years ago