# boar-server

> put these lines in your server.js ``` javascript var koa = require('koa'); var path = require('path'); var koaApp = module.exports = koa(); var config = require('./config'); var App = require('boar-server').app;

Latest version **9.3.1** (published 2026-08-05) · MIT license · 0 weekly downloads

## Install

```sh
npm install boar-server
pnpm add boar-server
yarn add boar-server
bun add boar-server
```

## Health

**Score 50/100 (C)** — status: active.

Positive: no vulnerabilities; has provenance.

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

## Facts

| | |
|---|---|
| Version | 9.3.1 |
| Published | 2026-08-05 |
| First published | 2015-11-09 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Node | >=18 |
| Dependencies | 11 |
| Unpacked size | 42.8 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Provenance | attested (GitHub Actions) |
| GitHub stars | 1 |
| Maintainers | mruell, akapa, kaaj, knagy, jfillmore, norbert-levajsics-emarsys, mmartin, bsoos, emarsys-deployer, drahos.istvan, judge, emarsys-security, daniel.banky, szeist, marton.papp.emarsys, dgyenes, s.viktor, m4w4q7, david.barkoczi, daniels1404, xin.he, qw3r, tamas.toth, gergaczd, viau, alkra, epgrubmair, bence.uto, ettancos, epmartini, gabor.balla.emarsys, mzsombor, ejpersson, llosonczy, iben12, kartonfarkas, adamoa, peva, bforgacs, zsomborh, kozma, ngabor84, andrasp3a, edosrecki, fhase, kkimak, gaborb, glendvai, lveraszto, bozsadam, rdoczi, fentosi, boristomic, mbazso, dmihalek, lhalasz, jason-nelson-01, dsztanko, tbugar, fqqdk, csabafarago-emarsys, sarakoll, mmolnar-emar, attila.gal, benjamingehl, rkumari03, tdorkaa, lkonya, lloki-emarsys, mrmeszaros, varszegik, i850773, dfarago, estefanlesnjakovic, mhunyady, atittel, marton.matusek, sixstep, saphendricksjoerg, mmothersill, jerryrichardson, arnaud.buchholz, jamescocker, adamszabolcs, mengjiao.zhao, ianhelmrich, mmartin2, dkorpos, jbleclerc, nvkaur2, tbucsanszki, pendicg24, pintera, asciortino1, nicolaeciumac, dimitrovn, maurogreco, bercziand, nish343, scotthetrick, konradschewe, ivanfroehlich, zbalazs, biamatei, oemlerm, mattfeldhake_emarsys, bborsi, sovagos, apoon, burci, mark.adorjan, xueboliang, skrivoo, kolosh, jviesersap, drewhodsonsap, criley, iulianmihai, mariannag, bobby_russel, dkocsis-emarsys, marko.fritzsche, demajo_ems, tothmarci25, tsiraitnpm, plsap, roxanams, bencekadaremar, viktor.szell, ccarrollem, geczirobert, sap-ns, dhruv-sap, irvansap, markjarvis, rollandgh, tillmannr, eggarcia, i517674, rimo86, arielkovacssap, sevket-ataseven, zholler, robertoraczemarsys, samthomas02, mfel0123, luca.fasolino.se, ndomke, franziskajung, andras.sarro, dschuppa, emarsys-stephen-ives, bencso, rcsullag, dputta, dwolter_emarsys, igordudassap, ahorvath1991, dr.j, jisaacson, tothbence8, d056437, ekkovats, lhammerl, iabraham, andras-marton-emarsys, sfariel, schroedersteffen, danielisap, tkatona-sap, boldizsarnagy, llakatos, gillyes, tade82, dpkemarsys, karlabrandl, tonyhsap, aidanlesh-sap, lucad2007, nikolett.tar, sridevir, abieler-sap, i534456, daniel.balazs, laszlo.ori, troywiegand, noahdojan, bcsizmadia, ariceem, csjakabos, borcsa, nnieman-sap, sap-am, vszegedi, ronnykrosse, taylor-seth, mrclsu, fcolombo-sap, mfawal, szb, aleksandrasap, mathischalland, ljanosi-sap, gschneider-sap, brandon-sap, atittelsap, barna.barak, pjohnson02, dominiknyerges, dcsutoras, bence.toth, weselyn1995, levente.otti.sap, torokhelen, ejwalker, matusekma, lvnte, jeno.pal, eadaniel, ppatel-emarsys, kialvatlan, obijoel-sap, mnguetsa, levente_szremko, ferenckalvinsap, aymenbidasap, banatti, ttoth2, vicabarta, sap_hang, kharitonoff, mwires_sap, azsoldi, ermafteiuscai, ivantorok, gyorgyabel, bence.nagy.emarsys, oliverweisenburger, erikgher, ktakacs, drilonjasharisap, peterforis-emarsys, nielsvbijsterveldt-emarsys, zsomborkincses, eergart, mfears, finally-fancy, magdafabian, samuel.varga_sap, yunyesap, aramovic, ronnie_sap, ivan.torok-e, fulopgabor, kovacs.zsolti.zsolt, szilvia.barnak, sachinmahajan, adurra, szilviabarnak, mfawall, mknura, juliaserr, peterlukacs, agrucza, gavinsun-sap, juliandietrich, nlazasap, johnnybakucz, bence-illia-emarsys, miklostorbics, znemes, rayhanwkapadia, farkasandras, benedek.izso.emarsys, balint-barki-sap, johannes_thiede_2001, 0505hendrk, florianmaak, eszter.kotra, hewayman, ruchidhiman-sap, blacksonic, sviktor, draven, adambrunner, gszabo |

## Links

- npm: https://www.npmjs.com/package/boar-server
- Repository: https://github.com/emartech/boar-server
- Issues: https://github.com/emartech/boar-server/issues
- npm.io page: https://npm.io/package/boar-server

## Dependencies (11)

- [koa-pug](https://npm.io/package/koa-pug.md) 5.1.1
- [koa-ssl](https://npm.io/package/koa-ssl.md) 2.0.1
- [koa-cors](https://npm.io/package/koa-cors.md) 0.0.16
- [koa-helmet](https://npm.io/package/koa-helmet.md) 7.0.2
- [koa-router](https://npm.io/package/koa-router.md) 13.0.1
- [koa-static](https://npm.io/package/koa-static.md) 5.0.0
- [app-root-path](https://npm.io/package/app-root-path.md) 3.1.0
- [koa-requestid](https://npm.io/package/koa-requestid.md) 2.3.0
- [koa-bodyparser](https://npm.io/package/koa-bodyparser.md) 4.4.1
- [koa-methodoverride](https://npm.io/package/koa-methodoverride.md) 2.0.0
- [@emartech/json-logger](https://npm.io/package/@emartech/json-logger.md) 8.0.2

## Recent versions

- 9.3.1 (latest) — 2026-08-05
- 9.2.0 — 2026-02-02
- 9.1.0 — 2024-11-12
- 9.0.0 — 2024-02-07
- 8.1.0 — 2024-01-16
- 8.0.0 — 2023-05-08
- 7.0.0 — 2021-05-21
- 6.0.0 — 2021-03-10
- 5.2.2 — 2018-05-09
- 5.2.1 — 2017-10-31
- 5.2.0 — 2017-08-07
- 5.1.0 — 2017-06-02
- 5.0.2 — 2016-09-22
- 5.0.1 — 2016-09-12
- 5.0.0 — 2016-09-01
- … 21 more at https://npm.io/package/boar-server/versions

## README

# Boar Server

## Example usage for app

put these lines in your server.js
``` javascript
  var koa = require('koa');
  var path = require('path');
  var koaApp = module.exports = koa();
  var config = require('./config');
  var App = require('boar-server').app;

  var app = new App(koaApp);
  app.connectToMongoose(config.mongooseUri);
  app.addDynamicViewMiddleware(path.join(config.root, '/views'), config.env === 'development');
  app.addStaticContentMiddleware(path.join(config.root, '/assets'));
  app.addHookMiddleware();
  app.loadControllers(path.join(config.root, 'controllers'));
  app.loadModels(path.join(config.root, 'models'));

  if (!module.parent) { app.listen(config.port); }
```

## Add middleware for your app
``` javascript
  var cors = require('koa-cors');
  var app = new App(koaApp);
  app.addMiddleware(cors());
```

## Graceful shutdown
You can stop the server from recieving new connections with `app.close()`. It returns a Promise that resolves when all existing connections are ended.
``` javascript
  var app = new App(koaApp);
  app.listen(config.port);
  process.on('SIGTERM', () => {
    app.close().then(() => {
      // additional cleaning (e.g. closing db connection)
      process.exit(0);
    })
  })
```

## HTTPS support
To enable HTTPS support, simple create `SERVE_HTTPS` environment variable with value `true`.
The port for https will be the port of the application increased with 10000 (10k).

If you want to serve the requests with your own SSL certification, create `HTTPS_KEY` and `HTTPS_CERT`
environment variables with path of the files as values.

### Example
```
export SERVE_HTTPS=true
export HTTPS_KEY="path/to/cert.key"
export HTTPS_CERT="path/to/cert.crt"

node server.js
```

## Build-in Middlewares

### Cors Support ([koa-cors](https://github.com/evert0n/koa-cors))

``` javascript
  app.addCorsSupportMiddleware();
```

### Static Content ([koa-static](https://github.com/koajs/static))

| Param | Type  | Description |
| ----- | ----- | ----------- |
| __path__ | `String` | Path to the static content's folder |

``` javascript
  app.addStaticContentMiddleware(path);
```

### Dynamic View

This middleware is a wrapper for [koa-pug](https://github.com/chrisyip/koa-pug).

| Param | Type  | Description |
| ----- | ----- | ----------- |
| __path__ | `String` | Path to the pug files |

``` javascript
  app.addDynamicViewMiddleware(path);
```

### Method Override ([koa-methodoverwrite](https://github.com/koa-modules/methodoverride))

``` javascript
  app.addMethodOverrideMiddleware();
```

### Error Handler

| Param | Type  | Description |
| ----- | ----- | ----------- |
| __path__ | `String` | Path to error page pug template |

``` javascript
  app.addErrorHandlerMiddleware(path);
```

### Body Parse ([koa-bodyparser](https://github.com/koajs/body-parser))

| Param | Type  | Description |
| ----- | ----- | ----------- |
| __options__ | `Object` | [More info.](https://github.com/koajs/bodyparser#options) |

``` javascript
  app.addBodyParseMiddleware(options);
```

### Request Id ([koa-requestid](https://github.com/seegno/koa-requestid))

| Param | Type  | Description |
| ----- | ----- | ----------- |
| __options__ | `Object` | _optional_ |
| ↳header | `String` | The name of the header to read the id on the request, `false` to disable. |
| ↳query  | `String` | The name of the header to read the id on the query string, `false` to disable. |
| ↳expose | `String` | The name of the header to expose the id on the response, `false` to disable. |

``` javascript
  app.addRequestIdmiddleware(options);
```

### Enforce SSL ([koa-ssl](https://github.com/jclem/koa-ssl))

| Param | Type  | Description |
| ----- | ----- | ----------- |
| __options__ | `Object` | [More info.](https://github.com/jclem/koa-ssl#use) |

``` javascript
  app.addEnforceSSLMiddleware();
```

If your application is running behind reverse proxy (like Heroku) you should set the trustProxy configuration option to *true* in order to process the x-forwarded-proto header.

``` javascript
  var app = new App(koaApp);
  app.addEnforceSSLMiddleware({ trustProxy: true });
```

__Note__: if you use this middleware EnforceSSL middleware should be the first you add.


### Hook

``` javascript
  app.addHookMiddleware();
```

### Security
Provides middlewares for setting up various security related HTTP headers.

| Param | Type  | Description |
| ----- | ----- | ----------- |
| __options__ | `Object` |  |
| ↳csp | `Object` | [More info.](https://github.com/helmetjs/csp) Learn more: [CSP quick reference](http://content-security-policy.com/) |
| ↳hsts | `Object` | [More info.](https://github.com/helmetjs/hsts) Learn more: [OWASP HSTS page](https://www.owasp.org/index.php/HTTP_Strict_Transport_Security) |
| ↳useXssFilter | `Boolean` | If `true`, [x-xss-protection](https://github.com/helmetjs/x-xss-protection) middleware will be included. Default: `true` |
| ↳useNoSniff | `Boolean` |  If `true`, [dont-sniff-mimetype](https://github.com/helmetjs/dont-sniff-mimetype) middleware will be included. Default: `true` |

``` javascript
  app.addSecurityMiddlewares(options);
```

#### Default configuration
``` javascript
  {
    csp: {
      directives: {
        defaultSrc: ["'self'"],
        scriptSrc: ["'self'"],
        styleSrc: ["'self'"],
        imgSrc: ["'self'"],
        frameAncestors: ["'self'"],
        reportUri: 'about:blank'
      },
      reportOnly: true
    },
    hsts: {
      maxAge: 30,
      includeSubdomains: true,
      preload: false
    },
    useXssFilter: true,
    useNoSniff: true
  }
```


## Libraries

### Mask email address
``` javascript
  var maskEmailAddress = require('boar-server').lib.maskEmailAddress;
  maskEmailAddress('test@gmail.com');
```

### Real ip address (in heroku)
``` javascript
  var realIpAddress = require('boar-server').lib.realIpAddress;
  realIpAddress(request);
```

### ControllerFactory
``` javascript
  var ControllerFactory = require('boar-server').lib.controllerFactory;

  module.exports = ControllerFactory.create(function(router) {
    router.get('/', ControllerFactory.load('main/actions/get'));
    router.get('/healthcheck', ControllerFactory.load('main/actions/healthcheck/get'));
    router.get('/list', ControllerFactory.loadByAcceptType('main/actions/list/get'));
  });
```

### ClearCollections

***deprecated*** aliased to `dropCollections`

Use the more descriptively named `dropCollections` instead.

### DropCollections

```javascript
  var dropCollections = require('boar-server').lib.dropCollections(mongoose);
  
  dropCollections(); // returns a promise
```

This will _drop_ all your collections.

### TruncateCollections

```javascript
  var truncateCollections = require('boar-server').lib.truncateCollections(mongoose);

  truncateCollections(); // returns a promise
```

This will _truncate_ all your collections.

### ClearGridfs

### Database

Wrapper for mongoose connection.

### ExceptionHandler

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