Giter Site home page Giter Site logo

hapi-plugin-co's Introduction

hapi-plugin-co

HAPI plugin for Co-Routine handlers

Installation

$ npm install hapi hapi-plugin-co

About

This is a small plugin for the HAPI server framework for seamless support of co-routines for route handlers, based on generator functions.

NOTICE

Although this plugin was ported to and works just with HAPI version >= 17.0.0 and Node.js version > 8.0.0, you are advised to use asynchronous functions with HAPI version >= 17.0.0's native support instead of generator functions and this HAPI plugin support. Generator functions and this HAPI plugin are actually from the HAPI version < 17.0.0 and Node.js version < 8.0.0 era.

Usage

The following sample server shows the plugin in action. It responds with { value: 42 } after waiting for a second.

var HAPI    = require("hapi")
var HAPICo  = require("hapi-plugin-co")
var Request = require("request-promise")

var server = new HAPI.Server({ debug: { request: [ "error" ] } })
server.connection({ address: "127.0.0.1", port: 12345 })
server.register(HAPICo, function () {

    /*  provider  */
    server.route({
        method:  "GET",
        path:    "/foo",
        handler: function * (request, reply) {  /*  THE CO-ROUTINE  */
            var value = yield new Promise(function (resolve, reject) {
                setTimeout(function () {
                    resolve(42)
                }, 1000)
            })
            reply({ value: value })
        }
    })

    server.start(function () {
        /*  consumer  */
        let p1 = server.inject({ method: "GET", url: "/foo" }).then(function (response) {
            if (response.result.value === 42)
                console.log("-- internal request: /foo: OK")
            else
                console.log("-- internal request: /foo: ERROR: invalid response: ", response.result.value)
        }).catch(function (error) {
            console.log("-- internal request: /foo: ERROR: ", error)
        })
        let p2 = Request({ uri: "http://127.0.0.1:12345/foo", json: true }).then(function (response) {
            if (response.value === 42)
                console.log("-- external request: /foo: OK")
            else
                console.log("-- external request: /foo: ERROR: invalid response: ", response.value)
        }).catch(function (error) {
            console.log("-- external request: /foo: ERROR: ", error)
        })
        Promise.all([ p1, p2 ]).then(function () {
            server.stop({ timeout: 1000 }, function (error) {
                if (error)
                    console.log("ERROR", error)
                process.exit(0)
            })
        })
    })

})

Notice

The smaller alternative: With hapi-async-handler there is an alternative HAPI plugin with similar functionality. The difference is that hapi-async-handler uses the official HAPI handler functionality to provide its functionality (and this way unfortunately causes more boilerplate code for the application then wished), does not recognize Boom error responses, requires an ES7 async function () (and not just an ES6 generator function function * ()) and especially does not use co internally (and this way cannot easily support the yielding of regular values, promises, sunks, etc).

The larger alternative: With hapi-co there is another alternative HAPI plugin with a much larger functionality. It allows the use of co-powered generator functions for all types of callbacks, including handlers. If you really need all this additional functionality, go with this module instead.

License

Copyright (c) 2016-2023 Dr. Ralf S. Engelschall (http://engelschall.com/)

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

hapi-plugin-co's People

Contributors

arthurmialon avatar jochenhoertreiter avatar rse avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

hapi-plugin-co's Issues

Promise not resolved with server.inject()

Hi
I am currently working on a project that utilize "co" on many places, and i find your hapi-plugin-co very useful, Thanks.

am using server.inject() to test my api the problem is routes with handler of function* not resolved or rejected, its stuck on pending state

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.