hapi-generator
Add generators and promises to your hapi handlers
Last updated a year ago by alanhoff .
MIT · Repository · Bugs · Original npm · Tarball
$ cnpm install hapi-generator 
SYNC missed versions from official npm registry.

hapi-generator

Add generators and promises to your hapi handlers

Usage

let server = new Hapi.Server()

// First register the plugin
server.register(require('hapi-generator'), err => {
  if (err) throw err
})

// Now you're free to use a generator or promise as a handler
server.route({
  method: 'POST',
  path: '/generator',
  handler: {
    generator: function * (request, reply) {
      let result = yield database.find()
      return result // Just return and the plugin will call reply() for you
    }
  }
})

server.route({
  method: 'POST',
  path: '/generator',
  handler: {
    promise: function (request, reply) {
      // Errors are automatically handled as well
      return database.find().then(() => { throw new Error('Whooops!') })
    }
  }
})

// If you wish to disable reply handling just pass reply: false
server.route({
  method: 'POST',
  path: '/home',
  handler: {
    generator: {
      reply: false,
      handler: function * (request, reply) {
        // Now the plugin will only call reply() for uncaught errors
        let data = yield database.find()
        reply.view('home', { data })
      }
    }
  }
})

Testing

git clone git@github.com:estate/hapi-generator.git
cd bookshelf-bcrypt && npm install && npm test
Maintainers (1)
Downloads
Today 0
This Week 0
This Month 0
Last Day 0
Last Week 0
Last Month 2
Dependencies (1)
  • co ^4.6.0
Dev Dependencies (6)
Dependents (0)
None

Copyright 2014 - 2017 © taobao.org |