Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

app-boot

Package Overview
Dependencies
Maintainers
1
Versions
5
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

app-boot

Boot an application.

  • 0.0.0
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

Boot

NPM version Build Status Coverage Status Dependencies

Boot an application.

Installation

$ npm install app-boot

Usage

var bootable = require( 'app-boot' );
bootable( [...args] )

Returns a boot function. All provided arguments are passed to each registered phase.

var app = require( 'express' )();

var boot = bootable( app );
boot.phase( fcn[, thisArg] )

Registers a new boot phase. On boot, phases sequentially execute according to registration order.

function phase1( app, next ) {
	// ...do something...
	next();
}

function phase2( app, next ) {
	var err;
	// ...do something...
	if ( err ) {
		return next( err );
	}
	next();
}

boot.phase( phase1 ).phase( phase2 );

Each phase receives all boot arguments plus an additional callback to invoke after a phase is complete.

var logger = require( './logger.js' ),
	config = require( './config.js' );

function phase( app, config, logger, next ) {
	logger.info( 'Here!' );
	next();
}

boot = bootable( app, config, logger );
boot.phase( phase );

To specify the this context for a phase, provide a thisArg.

var ctx = {
	'beep': 'boop'
};

function phase( app, config, logger, next ) {
	console.log( this.beep );
	// returns 'boop'

	next();
}

boot = bootable( app, config, logger );
boot.phase( phase, ctx );

To mimic async-waterfall, provide a locals object upon creating a boot sequence.

function phase1( app, locals, next ) {
	locals.beep = 'boop';
	next();
}

function phase2( app, locals, next ) {
	console.log( locals.beep );
	// returns 'boop'

	next();
}

boot = bootable( app, {} );
boot.phase( phase1 ).phase( phase2 );
boot( clbk )

Boots an application and invokes a callback once all phases complete.

boot( done );

function done( error ) {
	if ( error ) {
		throw error;
	}
	console.log( 'Finished booting...' );
}

Calling any phase's next callback with an error argument will cause the boot sequence to abort.

function phase1( app, next ) {
	// ...do something...
	next( new Error( 'phase1 error' ) );
}

function phase2( app, next ) {
	// ...never reached...
	next();
}

function done( error ) {
	if ( error ) {
		console.log( error.message );
		// returns 'phase1 error'
	}
}

boot = bootable( app );
boot.phase( phase1 ).phase( phase2 );
boot( done );

See Also

  • bootable
    • Whereas bootable binds an application to the phase this context, this module allows passing the application and any other parameters as arguments to each phase.
    • Rather than hang methods off (and thus mutate) the application, this module returns a function which wraps the application in a closure.
  • express
    • Similar to Express' middleware pattern, but with a more general interface.

Examples

var express = require( 'express' ),
	debug = require( 'debug' )( 'app-boot:example' ),
	bootable = require( 'app-boot' );

var boot, app;

// Bind middleware to the application...
function mw( app, next ) {
	debug( 'Binding middleware...' );
	app.get( '/beep', onRequest );
	next();
	function onRequest( request, response, next ) {
		console.log( 'Request received...' );
		next();
	}
}

// Mock connecting to a db...
function db( app, next ) {
	debug( 'Connecting to a database...' );
	process.nextTick( onConnect );
	function onConnect() {
		app.db = {};
		next();
	}
}

// Mock creating a server and listening on a port...
function server( app, next ) {
	debug( 'Creating a server and listening for requests...' );
	process.nextTick( onListen );
	function onListen() {
		app.server = {};
		next();
	}
}

// Callback invoked after completing a boot sequence...
function onReady( error ) {
	if ( error ) {
		throw error;
	}
	debug( 'Boot sequence completed...' );
}

// Create an application:
app = express();

// Create a boot function:
boot = bootable( app );

// Register phases:
boot.phase( mw )
	.phase( db )
	.phase( server );

// Boot the application:
boot( onReady );

To run the example code from the top-level application directory,

$ DEBUG=* node ./examples/index.js

Tests

Unit

Unit tests use the Mocha test framework with Chai assertions. To run the tests, execute the following command in the top-level application directory:

$ make test

All new feature development should have corresponding unit tests to validate correct functionality.

Test Coverage

This repository uses Istanbul as its code coverage tool. To generate a test coverage report, execute the following command in the top-level application directory:

$ make test-cov

Istanbul creates a ./reports/coverage directory. To access an HTML version of the report,

$ make view-cov

License

MIT license.

Copyright © 2015. Athan Reines.

Keywords

FAQs

Package last updated on 02 Oct 2015

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc