0.0.1 • Published 7 years ago

nicer-server v0.0.1

Weekly downloads
1
License
MIT
Repository
github
Last release
7 years ago

nicer-server

npm Node.js Build Status Coverage Dependencies Status devDependencies Status MIT licensed

A nicer HTTP(S) server.

Creates and manages an HTTP(S) server based on a specified callback function, maintaining a Map of active Socket connections to gracefully terminate them when the server closes. All methods return Promises.

Thoroughly tested with the built-in http@0.0.0 and https@1.0.0 modules, and compatible with the bluebird@3.5.0, express@4.15.4, koa@2.3.0, ws@3.1.0, and socket.io@2.0.3 modules.

Installation

npm install --save nicer-server

Usage

Basic HTTP server

const server = require('nicer-server');

let options = {
  port: 3000
};

server((req, res) => {
  res.end('Hello, world!');
}, options).listen();

Koa

const Koa    = require('koa');
const server = require('nicer-server');

let options = {
  port: 3000
};

let app = new Koa;

app.use(async (ctx) => {
  ctx.body = 'Hello, world!';
});

server(app.callback(), options).listen();

Express

const express = require('express');
const server  = require('nicer-server');

let options = {
  port: 3000
};

let app = express();

app.get('/', (req, res) => {
  res.end('Hello, world!');
});

server(app, options).listen();

API

#close

Destroys remaining sockets to terminate active connections, then closes the underlying HTTP(S) server.

#listen

Creates a new HTTP(S) server, adding event handlers to the request and connection events to toggle an idle flag on incoming Sockets and destroy them if necessary. Resolves once the server is listening.

#restart

Restarts the server. Calls #close, then #listen.

Options

const bluebird = require('bluebird');
const fs       = require('fs');

// Options for an HTTPS server listening on port 443,
// using the Bluebird Promise library
let options = {
  logger:  'https',
  port:    443,
  Promise: bluebird,
  ssl: {
    key:  fs.readFileSync('key.pem'),
    cert: fs.readFileSync('cert.pem')
  }
};

logger

nicer-server relies on the debug module for debugging messages. The name of the logger used by this module may be customized by specifying an optional string. Defaults to server.

port

The port to listen on. Defaults to 3000.

Promise

The constructor function to create internal Promises from. Defaults to the built-in Promise object.

ssl

An object of SSL options. Defaults to null. If null, the underlying server instance will be created using http#createServer. See the HTTPS Node.js documentation for more information.