node-app-base
Basic utilities for running node microservices
How to use
Require the base library and call it as a function, passing in your application name. The application name will be used to configure the various internal modules of the base, and must be alphanumeric with underscores only.
When instantiated, the base returns an object holding its modules, and it is recommended that you dereference these for use:
const base = 'example'const config logger metrics timers slack healthCheck = base
The base library is a singleton, so you can either pass it around through your application or simply call it again passing in the same application name.
See included example application for further details.
API examples
config
Provides access to safely read environment variables.
config // if foo is not overridden by an environment variableconfig // returns 5 // if bar is a defined environment variableconfig // returns bar, cast to a string // if bar is not a defined an environment variableconfig // throws error
Available values for the type
parameter:
string
int
,integer
,number
float
bool
,boolean
logger
Provides basic logging functions. The first argument should always be an alphanumeric key (dots and dashes are also allowed), and the optional second argument is a JSON object that is intended to provide context to the log line.
Note that there is intentionally no debug function, as we do not want to encourage divergence between the development and production environment.
logger logger logger
metrics
Provides an Prometheys-style instrumentation interface listening on port 9091 as standard. The module can be used to add additional metrics to the interface.
The operations below are all upserts, so they will create the metric for the first time if it does not already exist. This means that new metrics may appear in the instrumentation interface when new code paths have been hit.
See the npm prom-client module for further details.
metrics metrics metrics metricssummary name: 'response_time_percentile' help: 'Response time percentile distribution' percentiles: 001 01 09 099 value: responseTimeMs
timers
Provides high-resolution timers. Useful to use with logging.
const startToken = timersstart // do work const duration = timers console // an integer of the milliseconds elapsed since start was called.
slack
Allows the application to send messages to Slack.
This module relies on the following environment variables having been configured:
SLACK_URL: required, the webhook URL for slack
SLACK_CHANNEL: required: the channel or user at which to send messages
SLACK_USERNAME: optional, the username messages appear to have come from. Uses to the base app-name is not defined
SLACK_EMOJI: optional: the emoji icon for the slack user.
// if the required environment variables are definedslack // if the required environment variables are not definedslack
Health Check
Set up an HTTP endpoint to monitor application health. Returns 200 if health and 500 if not healthy.
The module can be configured as follows:
HEALTHCHECK_HTTP_LISTEN_PORT: optional, the listen port for the endpoint. Defaults to 8999
// Basic listenerhealthCheck;
// Advanced listener using promise { return { const isDatabaseOk = ; const isCacheOk = ; if isDatabaseOk && isCacheOk return ; return ; };} healthCheck;