Need Public Method

    a-server

    1.1.1 • Public • Published

    License: MIT Build Status

    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 = require('http');
     
    const server = http.createServer(function (req, res) {
        res.end('Hello beautiful world');
    });
     
    server.listen(8080, '127.0.0.1', function () {
        console.log('Listening at 127.0.0.1:8080');
    });

    With a-server :-)

    require('a-server')().start((req, res) => {
        res.end('Hello beautiful world');
    });
    • 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 = require('a-server');
    const myServer = aServer(options);
     
    myServer.start(function (req, res) {
        res.end('Hello beautiful world');
    });

    A Single Server

    If all you need is only one server, you can:

    const server = require('a-server')(options);
     
    server.start((req, res) => {
        res.end('Hello beautiful world');
    });

    or even:

    require('a-server')(options).start((req, res) => {
        res.end('Hello beautiful world');
    });

    Multiple Servers

    You can create multiple servers:

    const aServer = require('a-server');
     
    const myServer1 = aServer({port:8181});
    const myServer2 = aServer({port:8282});

    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 = require('a-server')();
    const myServer = aServer({
        timeout: 10000
        logs: false,
    });
     
    myServer.start();

    To change a-server's global defaults:

    const aServer = require('a-server');
     
    /* NOTE: This will be applied for ALL new a-server instances */
    aServer.defaults.timeout = 10000;
    aServer.defaults.logs    = 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 = require('a-server')(options);
     
    function app (req, res) {
        res.end('Hello beautiful world');
    }
     
    server.start(app);

    .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 = require('a-server')();
     
    function app (req, res) {
        res.end('Hello beautiful world');
    }
     
    server.start(app);
     
    // ...
     
    server.stop(function () {
        console.log('Goodbye!');
    });

    .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 = require('a-server')();
     
    function app_1 (req, res) {
        res.end('good morning!');
    }
     
    function app_2 (req, res) {
        res.end('good evening!');
    }
     
    aServer.start(app_1);
     
    // ...
     
    aServer.restart(app_2);

    .remount(newApp)

    Replaces the server's current request-handler with a new one.
    Does NOT call .stop() and.start() methods.

    example:

    const aServer = require('a-server')();
     
    function app_1 (req, res) {
        res.end('good morning!');
    }
     
    function app_2 (req, res) {
        res.end('good evening!');
    }
     
    aServer.start(app_1);
     
    // ...
     
    aServer.remount(app_2);

    .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 = require('a-server')();
     
    function app (req, res) {
        res.end('Hello beautiful world');
    }
     
    aServer.start(app);
     
    // ...
     
    aServer.kill();
     
    // ...
     
    aServer.start(app_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 = require('a-server')();
     
    myServer.onStart = function (myServer) {
        // e.g. OPEN a databse connection
    };
     
    myServer.onStop = function (myServer) {
        // e.g. CLOSE a databse connection
    };
     
    myServer.start((req, res) => {
        res.end('hello');
    });

    P.S.

    You have access to the underlying native HTTP server using the _server property:

        const aServer = require('a-server')();
     
        // the native HTTP server
        console.log(server._server);

    Install

    npm i a-server

    DownloadsWeekly Downloads

    2

    Version

    1.1.1

    License

    MIT

    Unpacked Size

    22 kB

    Total Files

    12

    Last publish

    Collaborators

    • taitulism