expedite

0.0.2 • Public • Published

Expedite 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 Expedite 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 expedite

Fast example

    var Expedite = require( 'expedite' );
 
    // Create a root endpoint that just returns the default JSON object with data set to "Thank you!"
    Expedite( "root" ).get( "/" ).then( ( req, res ) => res.ok( "Thank you!" ) );
 
    // Run the server
    Expedite.start();

Configuring a server

    var Server = require( 'expedite/server' );
 
    var config = {};
    config.port = 9001;
    config.name = "Hello World";
 
    var server = new Server( 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 Endpoint = require( 'expedite/http/endpoint' );
 
    // Endpoint instance that all actions will be attached to
    var root = new Endpoint( "root" );
 
    // Secure endpoint (using default policy)
    // @NOTE: 'then' is not a thenable, just a function convention
    root.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.
    root.post( "/" )
        .requireParam( "id" )
        .requireBodyParam( "name" )
        .then( function( req, res )
    {
        // An error alias allows more semantic error code
        res.error.internal( "NOT_IMPLEMENT" );
    } );
 
    root.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 = root;

WebSockets

Coming soon!

Mock Dummies

Coming soon!

Dependents (0)

Package Sidebar

Install

npm i expedite

Weekly Downloads

1

Version

0.0.2

License

MIT

Last publish

Collaborators

  • centaur2048