@kgryte/http-server

1.0.3 • Public • Published

HTTP Server

NPM version Build Status Coverage Status Dependencies

HTTP server.

Installation

$ npm install @kgryte/http-server

Usage

var httpServer = require( '@kgryte/http-server' );

httpServer( options, logger[, requestListener ] )

Returns a function to create an HTTP server.

var bunyan = require( 'bunyan' );

// Specify server options...
var opts = {
	'port': 7331,
	'address': '127.0.0.1'
};

// Create a logger...
var logger = bunyan.createLogger({
	'name': 'logger',
	'streams': [
		{
			'name': 'main',
			'level': 'info',
			'stream': process.stdout
		}
	]
});

// Create a function for creating an HTTP server...
var create = httpServer( opts, logger );

To bind a request callback to a server, specify a requestListener. For example,

var express = require( 'express' );

// Create a new application:
var app = express();

// Use the application as a request listener...
var create = httpServer( opts, logger, app );

The function accepts the following options:

  • port: server port. Default: 0 (i.e., randomly assigned).
  • maxport: max server port when port hunting. Default: maxport=port.
  • hostname: server hostname.
  • address: server address. Default: 0.0.0.0.

To specify a range of permissible ports, set the maxport option.

opts.maxport = 9999;

var create = httpServer( opts, logger, app );

When provided a maxport option, a created server will search for the first available port on which to listen, starting from port.

create( done )

Creates an HTTP server.

function done( error, server ) {
	if ( error ) {
		throw error;
	}
	console.log( 'Success!' );
	server.close();
}

create( done );

Notes

  • Port hunting can be useful in a microservice deployment. When a port is randomly assigned (options.port=0), if a server fails and is restarted, the server is unlikely to bind to its previous port. By allowing a constrained search, assuming no lower ports within a specified range have freed up in the meantime, the likelihood of listening on the same port is increased. A server can typically restart and bind to the same port faster than binding to a new port and re-registering with a microservice registry, thus minimizing possible service interruption and downtime.

Examples

var bunyan = require( 'bunyan' ),
	express = require( 'express' ),
	httpServer = require( '@kgryte/http-server' );

// Specify server options...
var opts = {
	'port': 7331,
	'maxport': 9999,
	'hostname': 'localhost'
};

// Create a logger...
var logger = bunyan.createLogger({
	'name': 'logger',
	'streams': [
		{
			'name': 'main',
			'level': 'info',
			'stream': process.stdout
		}
	]
});

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

// Create a function for creating an HTTP server...
var create = httpServer( opts, logger, app );

/**
* FUNCTION: done( error, server )
*	Callback invoked once a server is ready to receive HTTP requests.
*
* @param {Error|Null} error - error object
* @param {Server} server - server instance
* @returns {Void}
*/
function done( error, server ) {
	if ( error ) {
		throw error;
	}
	console.log( 'Success!' );
	server.close();
}

// Create a server:
create( done );

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

$ 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

Copyright © 2015. Athan Reines.

Package Sidebar

Install

npm i @kgryte/http-server

Weekly Downloads

6

Version

1.0.3

License

MIT

Last publish

Collaborators

  • kgryte