npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@magneds/hapi-server

v2.0.0

Published

Hapi based webserver

Downloads

4

Readme

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.