expedite
Framework for deploying Express and WebSocket enabled servers in an OO fashion
Last updated 3 years ago by centaur2048 .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install expedite 
SYNC missed versions from official npm registry.

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!

Current Tags

  • 0.0.2                                ...           latest (3 years ago)

2 Versions

  • 0.0.2                                ...           3 years ago
  • 0.0.1                                ...           3 years ago
Maintainers (1)
Downloads
Today 0
This Week 0
This Month 0
Last Day 0
Last Week 0
Last Month 1
Dependencies (13)
Dev Dependencies (1)
Dependents (0)
None

Copyright 2014 - 2016 © taobao.org |