Decorators for HapiJS routes
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.
.gitignore Use updated lodash find method May 30, 2018
.npmignore Add docpress to npm ignore Aug 19, 2016
.travis.yml Remove v4 and v5 as they are EOL by node Jun 14, 2018 chore(release): 1.0.0 Jun 15, 2018 docs: update the prerequisits Jun 15, 2018
docpress.json Add docpress config Aug 19, 2016
index.d.ts Remove support for config route setting Jun 2, 2018
index.js Remove support for config route setting Jun 2, 2018
package.json chore(release): 1.0.0 Jun 15, 2018


Decorators for HapiJS routes. Heavily inspired and borrowed from

Great to mix with

npm version Build Status Coverage Status



  • Hapi 17+ (Use 0.x for Hapi 16 or earlier)
  • Node 6+ (Use 0.x for Node 5 or earlier)
npm install --save hapi-decorators
import {
} from 'hapi-decorators'
import Hapi from 'hapi'

const server = new Hapi.Server()

  host: 'localhost',
  port: 3000

// Define your endpoint controller
class TestController {
  constructor(target) { = target

  sayHello(request, reply) {
    reply({ message: `hello, ${}` })

// InitializeController
let test = new TestController('world')

// Add Test Controller routes to server

// Start the server
server.start((err) => {
  if (err) throw err
  console.log(`Server running at: ${}`)

Setup Babel

Run the above script with the following command, after installing babel.

babel-node --optional es7.decorators,es7.objectRestSpread index.js

Note: Decorators are currently unsupported in Babel 6. To work around that issue use the transform-decorators-legacy plugin. See this post for detailed instructions.



REQUIRED This decorator is required at the class level, since it processes the other decorators, and adds the instance.routes() function, which returns the routes that can be used with Hapi, e.g. server.routes(users.routes()).

@route(method, path)

This decorator should be attached to a method of a class, e.g.

class Users {
  @route('post', '/')
  newUser(request, reply) {

Helper Decorators

  • @get(path)
  • @post(path)
  • @put(path)
  • @patch(path)
  • @delete(path)
  • @del(path)
  • @all(path)

These are shortcuts for @route(method, path) where @get('/revoke') would be @route('get', '/revoke').


Overall options setting if none of the other decorators are sufficient.


Add a validation object for the different types, except for the response. config is an object, with keys for the different types, e.g. payload.


Cache settings for the route config object.


Set prerequisite middleware array for a given route. Expects an array, but if passed something else, it will put it into the pre array.