https://github.com/connorwiseman/nicer-server
A nicer HTTP(S) server.
https://github.com/connorwiseman/nicer-server
http https nodejs server
Last synced: 10 months ago
JSON representation
A nicer HTTP(S) server.
- Host: GitHub
- URL: https://github.com/connorwiseman/nicer-server
- Owner: ConnorWiseman
- License: mit
- Created: 2017-09-14T04:03:45.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2018-06-26T16:05:04.000Z (over 7 years ago)
- Last Synced: 2025-03-02T00:48:39.359Z (11 months ago)
- Topics: http, https, nodejs, server
- Language: JavaScript
- Homepage:
- Size: 71.3 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# nicer-server
[](https://www.npmjs.com/package/nicer-server)

[](https://travis-ci.org/ConnorWiseman/nicer-server) [](https://codecov.io/gh/ConnorWiseman/nicer-server)
[](https://david-dm.org/ConnorWiseman/nicer-server)
[](https://david-dm.org/ConnorWiseman/nicer-server?type=dev)
[](https://github.com/ConnorWiseman/nicer-server/blob/master/LICENSE)
> 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`](https://nodejs.org/api/http.html) and [`https@1.0.0`](https://nodejs.org/api/https.html) modules, and compatible with the [`bluebird@3.5.1`](https://github.com/petkaantonov/bluebird/), [`express@4.16.3`](https://github.com/expressjs/express), [`koa@2.5.1`](https://github.com/koajs/koa), [`ws@5.2.1`](https://github.com/websockets/ws), and [`socket.io@2.1.1`](https://github.com/socketio/socket.io) modules.
## Installation
```shell
npm install --save nicer-server
```
## Usage
### Basic HTTP server
```javascript
const server = require('nicer-server');
let options = {
port: 3000
};
server((req, res) => {
res.end('Hello, world!');
}, options).listen();
```
### Koa
```javascript
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
```javascript
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
```javascript
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`](https://github.com/visionmedia/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](https://nodejs.org/api/https.html#https_https_createserver_options_requestlistener) for more information.