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

@magneds/hapi-server

Package Overview
Dependencies
Maintainers
3
Versions
6
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@magneds/hapi-server

Hapi based webserver

  • 2.0.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
3
Created
Source

Node Hapi Server

A configurable webserver package for easy bootstrapping a full blown webserver, microservice or anything in between.

Installation

The HapiServer package is a scoped packages, which means you'll have to include the scope during installation.

$ npm install --save @magneds/hapi-server

Usage

As with the installation, the scope is required to use the package as well.

const HapiServer = require('@magneds/hapi-server');

new HapiServer()
	//  configure the server (call as many times as needed, existing value will
	//  be overwritten)
	.configure(/* hapi server configuration option(s) */)

	//  add plugins (provide plugin objects, or arrays of them, call as often
	//  as needed)
	.plugin(/* any plugin */)

	//  add routes (provide route objects, or arrays of them, call as often
	//  as needed)
	.route(/* any route */)

	//  start the server
	.start()

	//  do something relevant once started (or catch errors)
	.then((server) => {
		console.log(`Server running at: ${server.info.uri}`);
	});

API

HapiServer allows for a mixture of methods, whatever floats your boat, the only real requirement is to add at least the configuration of host and port (and even that is - as per Hapi - not mandatory, it just makes predicting the host and port more consistent) and as final call the start method.

configure(...<object)

Set server configuration options. This method can be called multiple times, where it will overwrite any existing configuration option.

const HapiServer = require('@magneds/hapi-server');

new HapiServer()
	.configure({ host: 'localhost', port: 30080 })
	.configure({ port: 3000 }) //  override the port to be 3000
	//...
	.start();

plugin(<object>|[<object>])

Register one or more Hapi compatible plugins. It allows for both objects and arrays, both also in a variadic (spread, splat, ...) way. Any mix of the following calls will have the same result:

  • plugin(PluginOne, PluginTwo)
  • plugin([PluginOne, PluginTwo])
  • plugin(...[PluginOne, PluginTwo])
const HapiServer = require('@magneds/hapi-server');
const MyFirstPlugin = require('@my/first-plugin');
const MySecondPlugin = require('@my/second-plugin');
const MyThirdPlugin = require('@my/third-plugin');
const MyFourthPlugin = require('@my/fourth-plugin');

new HapiServer()
	.configure({ host: 'localhost', port: 3000 })
	.plugin(MyFirstPlugin)
	.plugin(MySecondPlugin)
	.plugin(MyThirdPlugin)
	.plugin(MyFourthPlugin)
	//...
	.start();

route(<object>|[<object>])

Register one or more Hapi compatible routes. It allows for both objects and arrays, both also in a variadic (spread, splat, ...) way. Any mix of the following calls will have the same result:

  • route({ method: 'GET', path: '/one', handler(request, h) { return h.response('one'); }}, ...)
  • route([{ method: 'GET', path: '/one', handler(request, h) { return h.response('one'); }}, ...])
const HapiServer = require('@magneds/hapi-server');

new HapiServer()
	.configure({ host: 'localhost', port: 3000 })
	.route({
		method: 'GET',
		path: '/one',
		handler(request, h) {
			return h.response('one');
		}
	})
	.route([
		{
			method: 'GET',
			path: '/two',
			handler(request, h) {
				return h.response('two');
			}
		},
		{
			method: 'GET',
			path: '/three',
			handler(request, h) {
				return h.response('three');
			}
		}
	])
	.route(
		{
			method: 'GET',
			path: '/four',
			handler(request, h) {
				return h.response('four');
			}
		},
		{
			method: 'GET',
			path: '/five',
			handler(request, h) {
				return h.response('five');
			}
		}
	)
	//...
	.start();

start()

As the name indicates, this will start the server. All of the configuration options, plugins and routes are prepared at this point and the Hapi server is started and provided in the Promise resolve.

const HapiServer = require('@magneds/hapi-server');

new HapiServer()
	//...
	.start()
	.then((server) => {
		console.log(`Server running at: ${server.info.uri}`);
	})
	.catch((error) => {
		console.error(error);
	});

Change log

Please see Changelog

Testing

$ npm test

Contributing

Please see Contributing

Credits

License

The MIT License (MIT). Please see License File for more information.

Keywords

FAQs

Package last updated on 20 Feb 2020

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