# cnn-hapi

> This is a generic Hapi server for starting projects.

Latest version **2.5.2** (published 2018-04-25) · MIT license · 0 weekly downloads

## Install

```sh
npm install cnn-hapi
pnpm add cnn-hapi
yarn add cnn-hapi
bun add cnn-hapi
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 2.5.2 |
| Published | 2018-04-25 |
| First published | 2016-02-06 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Node | >=8.9.4 |
| Dependencies | 11 |
| Unpacked size | 94.2 KB |
| Known vulnerabilities | 0 (+1 in 1 direct dependencies) |
| Install scripts | no |
| GitHub stars | 10 |
| Maintainers | adslaton, alexsover, britneydossett, cnnlabs, codeclout, drenter, jamsyoung, khowen, matthewjon |
| Keywords | cnn, cnnlabs, hapi |

## Links

- npm: https://www.npmjs.com/package/cnn-hapi
- Repository: https://github.com/cnnlabs/cnn-hapi
- Homepage: https://github.com/cnnlabs/cnn-hapi#readme
- Issues: https://github.com/cnnlabs/cnn-hapi/issues
- npm.io page: https://npm.io/package/cnn-hapi

## Dependencies (11)

- [good](https://npm.io/package/good.md) 7.3.0
- [hapi](https://npm.io/package/hapi.md) 16.6.3
- [debug](https://npm.io/package/debug.md) 3.1.0
- [inert](https://npm.io/package/inert.md) 4.2.1
- [podium](https://npm.io/package/podium.md) 1.3.0
- [vision](https://npm.io/package/vision.md) 4.1.1
- [cnn-health](https://npm.io/package/cnn-health.md) 0.2.1
- [cnn-metrics](https://npm.io/package/cnn-metrics.md) 0.5.0
- [good-console](https://npm.io/package/good-console.md) 6.4.1
- [good-squeeze](https://npm.io/package/good-squeeze.md) 5.0.2
- [hapi-swagger](https://npm.io/package/hapi-swagger.md) 8.0.0

## Recent versions

- 2.5.2 (latest) — 2018-04-25
- 2.4.3 — 2018-03-02
- 2.4.2 — 2017-09-13
- 2.4.1 — 2017-09-13
- 2.3.0 — 2017-07-07
- 2.2.0 — 2017-06-19
- 2.1.0 — 2017-04-08
- 2.0.0 — 2017-03-17
- 1.2.0 — 2017-03-16
- 1.1.2 — 2016-10-13
- 1.1.1 — 2016-09-27
- 0.12.2 — 2016-09-16
- 1.0.0 — 2016-09-15
- 0.12.1 — 2016-08-22
- 0.12.0 — 2016-08-22
- … 28 more at https://npm.io/package/cnn-hapi/versions

## README

# CNN Hapi

Basic [Hapi](http://hapijs.com/) server with some baked in features that can be
pulled in as a dependency of another application to extend as needed.

[![build](https://img.shields.io/travis/cnnlabs/cnn-hapi/master.svg?style=flat-square)](https://travis-ci.org/cnnlabs/cnn-hapi)
![node](https://img.shields.io/node/v/cnn-hapi.svg?style=flat-square)
[![npm](https://img.shields.io/npm/v/cnn-hapi.svg?style=flat-square)](https://www.npmjs.com/package/cnn-hapi)
[![npm-downloads](https://img.shields.io/npm/dm/cnn-hapi.svg?style=flat-square)](https://www.npmjs.com/package/cnn-hapi)
[![dependency-status](https://gemnasium.com/cnnlabs/cnn-hapi.svg)](https://gemnasium.com/cnnlabs/cnn-hapi)

Features include:

- Swagger on /documentation
- Basic healthcheck on /__health
- Basic logging
- Basic metrics
- Basic process monitoring
- Default Cache/Surrogate Control headers
- Default custom headers


## Requirements

[Node 6.0.0+](https://npmjs.org)


## Installation

```shell
$ npm install
```


## Usage

Look at the [/example/app.js](./example/app.js) to see an example of how this
can be pulled in as a dependency.  You can see it running by doing the
following.

```shell
$ PORT=5000 node example/app.js
info Server running at http://0.0.0.0:5000
info Server name: testHarness
info Server version: 0.1.0
info Server maxListeners: 1000
info Server environment: development
info Server in debug mode: true
160915/014027.438, [ops] memory: 65Mb, uptime (seconds): 5.705, load: [1.30322265625,1.486328125,1.5888671875]
160915/014032.438, [ops] memory: 58Mb, uptime (seconds): 10.706, load: [1.35888671875,1.49462890625,1.5908203125]
```

You can also navigate to localhost:5000 and see a served page.

Swagger documentation - `localhost:5000/documentation`

Healthcheck monitoring - `localhost:5000/__health`

## Testing
`$ cd <cnn-hapi-root>`
`$ npm run example-server`
This will run the example server in the `./example` directory.

## ENV VARS
+ `LOADER_IO_VALIDATION`

+ `PORT`

+ `LOCAL_TLS_PORT`

+ `CACHE_CONTROL`

+ `ENVIRONMENT`

+ `HOST`

+ `DEFAULT_MAX_LISTENERS`

+ `SURROGATE_CACHE_CONTROL`

+ `SHOW_CNN_HAPI_CONFIG`  => Setting this to 'true' will show server instance configurations on `server.start()`. Requires `DEBUG=cnn-hapi*` to be a part of `DEBUG` capture group<br>

+ `METRICS_FLUSHEVERY`<br>


## serverInstance(options)
_The following options set defaults at the server level and can override CNN-Hapi Defaults_
_Populate notes are in order of priority. Example: populate: `process.env.SOMEVALUE` || `options.someValue`. In that example if `process.env.SOMEVALUE` is not set it will default to `options.someValue`, etc, etc_
_Manual override possibilites are expressed in `options.someValue`_

_`options` is an object that can take the following keys_

+ `basePath`: project basePath,

+ `cacheControlHeader`: process.env.CACHE_CONTROL || 'max-age=60',

+ `customHeaders`: options.customHeaders || [],

+ `description`: options.description || `package.json` `description` key,

+ `environment`: process.env.ENVIRONMENT || process.env.NODE_ENV || options.environment || '',

+ `healthChecks`: options.healthChecks || [],

+ `host`: process.env.HOST || options.host || '0.0.0.0',

+ `loaderIoValidationKey`: options.loaderIoValidationKey || undefined,

+ `localTLS`: options.localTLS || null,

+ `maxListeners`: process.env.DEFAULT_MAX_LISTENERS || options.maxListeners || 10,

+ `name`: options.name || `package.json` `name` key,

+ `port`: process.env.PORT || options.port || 3000,

+ `surrogateCacheControl`:
    process.env.SURROGATE_CACHE_CONTROL || options.surrogateCacheControl || 'max-age=360, stale-while-revalidate=60, stale-if-error=86400',

+ `version`:options.version || `package.json` `version` key,

+ `withGoodConsole`: options.withGoodConsole || false,

+ `withSwagger`: options.withSwagger || false

## Override caching on individual routes
_Using the reply.header() function can set headers on a singular route_

```
{
    method: 'GET',
    path: '/override-headers',
    handler: (request, reply) => {
      reply('Peep the response headers in swagger docs')
      .header('Cache-Control', '2')
      .header('Surrogate-Control', 'baz');
    },
    config: {
      description: 'Example route for demonstrating how to  override headers by route',
      tags: ['api']
    }
  },
  ```
_For explicit usage check this implementation in `./example/routes`_


# Test

`$ npm run example-server`

It runs example server located in `./example`. Pass in ENV vars through the above command or hardcode into the package.json located in the `./example` directory.

Go to {HOST}:{PORT}/documentation to view the new swagger docs and to test current `CNN-Hapi` logic.

---
_Source: https://npm.io/package/cnn-hapi · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
