hapijs / glue

Server composer for hapi.js
Other
245 stars 62 forks source link

Cant get a basic NodeJS script working with HapiJS & Glue #48

Closed jhyland87 closed 8 years ago

jhyland87 commented 8 years ago

I'm reading the Developing a Hapi Edge book, and wanted to try to get a basic HapiJS API server going using Glue, the JS is pretty much right from the book, and even took some of the plugins out, but it looks like it should work: http://pastebin.com/x6Wftvhd (Transpiled ES5 version: http://pastebin.com/PH9qiFut)

However, it returns an error in the console:

$ node dist/app.js 
/Users/me/Documents/scripts/js/node/test/hapi/hapi_server/node_modules/glue/node_modules/joi/lib/index.js:141
                throw error;
                ^

ValidationError: Invalid manifest {
  "connections": [
    {
      "port": 8080,
      "labels": [
        "http"
      ]
    },
    {
      "port": 8088,
      "labels": [
        "api"
      ]
    }
  ],
  "plugins" [1]: {
    "good": {
      "opsInterval": 5000,
      "reporters": [
        {
          "reporter": "good-console",
          "events": {
            "ops": "*",
            "log": "*"
          }
        }
      ]
    }
  }
}

[1] "plugins" is not allowed
    at Object.internals.Err.toString.exports.process (/Users/me/Documents/scripts/js/node/test/hapi/hapi_server/node_modules/glue/node_modules/joi/lib/errors.js:140:19)
    at internals.Any.applyFunctionToChildren.internals.Any._validateWithOptions (/Users/me/Documents/scripts/js/node/test/hapi/hapi_server/node_modules/glue/node_modules/joi/lib/any.js:654:27)
    at root.validate (/Users/me/Documents/scripts/js/node/test/hapi/hapi_server/node_modules/glue/node_modules/joi/lib/index.js:102:23)
    at root.attempt (/Users/me/Documents/scripts/js/node/test/hapi/hapi_server/node_modules/glue/node_modules/joi/lib/index.js:131:29)
    at root.assert (/Users/me/Documents/scripts/js/node/test/hapi/hapi_server/node_modules/glue/node_modules/joi/lib/index.js:126:14)
    at Object.exports.compose.steps.push.steps.push.steps.push.steps.push.Items.serial [as compose] (/Users/me/Documents/scripts/js/node/test/hapi/hapi_server/node_modules/glue/lib/index.js:43:9)
    at Object.<anonymous> (/Users/me/Documents/scripts/js/node/test/hapi/hapi_server/dist/app.js:60:6)
    at Module._compile (module.js:399:26)
    at Object.Module._extensions..js (module.js:406:10)
    at Module.load (module.js:345:32)

Any ideas?

gergoerdosi commented 8 years ago

It should be registrations, see the example in the usage section:

https://github.com/hapijs/glue/blob/master/API.md#usage

devinivy commented 8 years ago

The issue is that glue v3 recently came out, and it's likely that you're reading a tutorial for v2. The format of the glue manifest has changed between those two versions.

jhyland87 commented 8 years ago

Literally RIGHT when I posted the above, Is came across the same API link you posted above on my own... Thank you.

Is there a good book for V3 you would recommend?

vdeturckheim commented 8 years ago

@jhyland87, V3 as been released yesterday, it is pretty unlikely that any book has taken the new format in account yet. However you can just use the v2 for now.

jhyland87 commented 8 years ago

@vdeturckheim, eh, ill use V3, and ill continue to read the book, ill just make sure to look up the documentation for anything I look at.

Thanks!

jhyland87 commented 8 years ago

So I got the basics to work, but it doesn't look like the labels in the registration[ # ].plugin.options.select are being applied..

'use strict';

import * as Hapi from 'hapi';
import * as Glue from 'glue';

const manifest = {
    server: {
        //cache: 'redis'
    },
    connections: [
        {
            port: 8000,
            labels: ['api']
        },
        {
            port: 8001,
            labels: ['http']
        }
    ],
    registrations: [
        {
            plugin: {
                register: './routes',
                options: {
                    select: [ 'api' ]
                }
            }
        }
    ]
};

const options = {
    relativeTo: __dirname
};

Glue.compose(manifest, options, (err, server) => {
    if (err) throw err;

    server.start(() => {
        console.log('hapi days!');
    });
});

Then the routes.js is:

// routes.js
'use strict';

exports.register = (server, options, next) => {
    server.route({
        method: 'GET',
        path: '/hello-world',
        handler: (request, reply) => {
            reply( 'test passed' );
        }
    });

    next();
};

exports.register.attributes = {
    pkg: {
        name: "helloWorld",
        version: "0.1.0"
    }
};

However the /hello-world works for both ports, am I doing something wrong? (im sure)

gergoerdosi commented 8 years ago

The code in glue doesn't look good to me. Created a PR to fix this.

jhyland87 commented 8 years ago

Oh ok, I thought I was going crazy.

Thanks!

csrl commented 8 years ago

You want to pass that as a registration option, not a plugin init option. ie.

    registrations: [
        {
            plugin: {
                register: './routes',
            },
            options: {
                select: [ 'api' ]
            }
        }
    ]

See https://github.com/hapijs/glue/blob/master/API.md

lock[bot] commented 4 years ago

This thread has been automatically locked due to inactivity. Please open a new issue for related bugs or questions following the new issue template instructions.