DEPRECATED
Keep calm and move on.
This module is one of my earliest gigs and is mostly unjustified overhead.
a-server
A dead-simple HTTP server class. A wrapper to Node's native HTTP server, enables you to easly create and control an HTTP server.
Without a-server :-(
const http = ; const server = http; server;
With a-server :-)
start { res;};
- A server with a dead simple API (
.start()
,.stop()
etc). - Zero configuration for basic usage.
- Yet, configurable.
Install
$ npm install a-server --save
Usage
const aServer = ;const myServer = ; myServerstart { res;};
A Single Server
If all you need is only one server, you can:
const server = options; serverstart { res;};
or even:
optionsstart { res;};
Multiple Servers
You can create multiple servers:
const aServer = ; const myServer1 = ;const myServer2 = ;
Options
host
<String>
Begin accepting connections on the specified hostname. If the hostname is null
, the server will accept connections on any IPv6 address (::)
Default: process.env.IP (if exists) or '127.0.0.1'
.
port
<Number>
Begin accepting connections on the specified port.
Default: process.env.PORT (if exists) or 8080
.
https
<Object>
An HTTPS options object. Read more.
Default: false
(HTTP server)
timeout
<Number>
Sockets idle timeout limit in milliseconds (Node's default is 2 minutes)
Default: 5000
logs
<Boolean>
Basic default logs when the server starts, when it stops and when a new app is being mounted.
Default: true
Global options vs. Own options
To setup a new a-server instance with its own individual options:
const aServer = ;const myServer = ; myServerstart;
To change a-server's global defaults:
const aServer = ; /* NOTE: This will be applied for ALL new a-server instances */aServerdefaultstimeout = 10000;aServerdefaultslogs = false;
API Methods
.start(app)
Start listening for client requests.
app
is a request-handler, a function for handling the request
and the response
.
After the server has started the .onStart()
hook function gets called (see hooks below).
example:
const server = options; { res;} serverstartapp;
.stop([callback])
Stops the server from accepting new requests.
callback
(function) is an optional argument. It will get called after the .onStop()
hook (see hooks below).
example:
const server = ; { res;} serverstartapp; // ... server;
.restart([newApp])
Stops the server and starts it again with an optional new app function (a new request-handler).
Calls .stop()
and .start()
methods (meaning: runs the onStop
and onStart
hooks. Read more about hooks.
example:
const aServer = ; { res;} { res;} aServerstartapp_1; // ... aServer;
.remount(newApp)
Replaces the server's current request-handler with a new one.
Does NOT call .stop()
and.start()
methods.
example:
const aServer = ; { res;} { res;} aServerstartapp_1; // ... aServer;
.kill()
Stops the server from accepting new requests and kill its props and handlers. Calls the native server.unref()
.
A killed server cannot be started again.
example:
const aServer = ; { res;} aServerstartapp; // ... aServer; // ... aServerstartapp_1; // --> error
Hooks
NOTE: a-server is NOT an instance of EventEmitter.
a-server has two event-like hooks:
onStart
type: <Function>
default: None.
A callback function to run after the server starts.
Gets called with the server
instance as its only argument.
onStop
type: <Function>
default: None.
A callback function to run after the server stops.
Gets called with the server
instance as its only argument.
Simply put your callback functions in those placeholders:
const myServer = ; myServer { // e.g. OPEN a databse connection}; myServer { // e.g. CLOSE a databse connection}; myServerstart { res;};
P.S.
You have access to the underlying native HTTP server using the _server
property:
const aServer = ; // the native HTTP server console;