blitz-server

0.0.1 • Public • Published

Blitz Server

A simple, fast wrapper for getting Express HTTP and WebSocket servers up, running, and maintained.

Why?

Express is nice and concise and that is exactly what a lot of minimalist, simple services need. But, in larger projects, there are a lot of decisions to be made about how to best used express. The goal of Blitz Server is to create both a minimalist framework on top of Express AND provide large-project level abstractions for ease of separation and interaction.

Installation

$ npm install blitz-server

Fast example

    var Blitz = require( 'blitz' );

    // Create a root endpoint that just returns the default JSON object with data set to "Thank you!"
    Blitz( "root" ).get( "/" ).then( ( req, res ) => res.ok( "Thank you!" ) );

    // Run the server
    Blitz.start();

Configuring a server

    var BlitzServer = require( 'blitz/server' );

    var config = {};
    config.port = 9001;
    config.name = "Hello World";

    var server = new BlitzServer( config );

    server.setDefaultSecurityPolicy( function( req, done )
    {
        if ( req.query.id === "myadminname" )
        {
            // Return true to indicate that the security
            // policy has been met.
            done( true );
            return;
        }

        // False indicate the security policy has NOT been met
        // and will cause an unauthorized access message.
        done( false );
    } );

    // Alternate configurations can be supplied
    var devConfig = {};
    devConfig.environment = "dev";

    // Merged configurations will look through the arguments used to spawn
    // the process and determine if they should be merged into the server's
    // configuration object.

    // If this server was started with the arg 'dev', then the specified config
    // will be merged in, otherwise discarded.
    server.mergeConfigs( { dev : devConfig } );

    // Endpoints should be added individually
    server.addEndpoint( require( './rootendpoint' ) );

    server.start();

Building an endpoint

    var BlitzEndpoint = require( '../../http/endpoint' );

    // Endpoint instance that all actions will be attached to
    var rootEndpoint = new BlitzEndpoint( "root" );

    // Secure endpoint (using default policy)
    // @NOTE: 'then' is not a thenable, just a function convention
    rootEndpoint.get( "/" ).setSecure( true ).then( function( req, res, config )
    {
        var data = {};
        data.message = "hello world";
        data.config = config;

        // ok function is used for an OK response (everything went OK)
        res.ok( data );
    } );

    // Non-secure endpoint posting to the same root URL,
    // note that 'id' is required as part of the query parameter
    // and 'name' is required as part of the posted body.
    rootEndpoint.post( "/" )
        .requireParam( "id" )
        .requireBodyParam( "name" )
        .then( function( req, res )
    {
        // An error alias allows more semantic error code
        res.error.internal( "NOT_IMPLEMENT" );
    } );

    rootEndpoint.put( "/" ).then( function( req, res )
    {
        // Various error types exist with reasonable error code mappings
        res.error.security( "You are not admin" );
    } );

    // Endpoint is exported for requiring in by the entry point module (see: server above)
    module.exports = rootEndpoint;

WebSockets

Coming soon!

Mock Dummies

Coming soon!

Dependencies (13)

Dev Dependencies (1)

Package Sidebar

Install

npm i blitz-server

Weekly Downloads

1

Version

0.0.1

License

MIT

Last publish

Collaborators

  • centaur2048