nicer-server
v0.0.1
Published
A nicer HTTP(S) server.
Downloads
3
Readme
nicer-server
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 [email protected]
and [email protected]
modules, and compatible with the [email protected]
, [email protected]
, [email protected]
, [email protected]
, and [email protected]
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.