This package contains some handy utility methods that I use in my hapi.js (v20) server applications.
The package is not in the npm registry (don’t think it’s general-purpose enough), but you can install it from GitHub:
npm install -S peruukki/hapi-server-utils#v3.0.0
Starts a hapi.js server with Basic Authentication (using @hapi/basic), serving static files (using @hapi/inert) and request proxying (using @hapi/h2o2).
Basic Authentication is enabled by default, but can be disabled by defining the environment variable
BASIC_AUTH_DISABLED=true
. If it is enabled, all requests are authenticated and access is allowed with
the credentials from the environment variables ADMIN_USERNAME
and ADMIN_PASSWORD
(see lib/auth.js
).
The server serves given routes
and all route handlers are passed the hapi.js server instance and the given
config
value, see the example below.
The server listens on port from the environment variable PORT
or the given defaultPort
.
The optional options
object can contain:
loggingOptions
: additional hapi-pino options to override the defaultsstaticFilesDir
: if given, static files are served from that directory. Pass e.g.path.join(__dirname, 'public')
if you have a top-levelpublic
directory.
// server.js
const { env, server } = require('hapi-server-utils');
const path = require('path');
const routes = require('./routes');
const config = { apiHost: env('API_HOST', 'http://127.0.0.1:8081') };
const staticFilesDir = path.join(__dirname, 'public');
server(routes, config, '8080', { staticFilesDir });
// routes.js
module.exports = function routes(server, config) {
// Proxy API requests
server.route({
method: ['GET', 'POST', 'PUT', 'PATCH', 'OPTIONS'],
path: '/api/{param*}',
handler: {
proxy: {
uri: `${config.apiHost}/api/{param}`,
passThrough: true,
},
},
});
};
Similar to server
but doesn't actually start a server, just returns the value from the Hapi.server()
call.
Useful in tests with e.g. supertest.
// app.js
const { app } = require('hapi-server-utils');
const routes = require('../../../routes');
const config = {
/* Some test config */
};
module.exports = app(routes, config, '8080');
// assertions.js
const request = require('supertest');
module.exports = async function assertResponse(app, requestUrl, statusCode) {
const server = await app;
return request(server.listener)
.get(requestUrl)
.expect(statusCode)
.expect('Content-Type', 'application/json; charset=utf-8');
};
Returns the value of the environment variable denoted by key
, or defaultValue
if the environment
variable is not defined. Just a wrapper for non-prefixed
habitat#get.
const { env } = require('hapi-server-utils');
const apiHost = env('API_HOST', 'http://127.0.0.1:8080');
Run basic tests and lint the JavaScript with ESLint:
npm test
The code is formatted using Prettier (see .prettierrc
for the configuration).
This project has been a grateful recipient of the Futurice Open Source sponsorship program.