Hapi: Confusing V17 documentation

Created on 20 Nov 2017  ·  11Comments  ·  Source: hapijs/hapi

https://hapijs.com/api/17.0.1#server.ext() has a code:

const Hapi = require('hapi');
const server = Hapi.server({ port: 80 });

server.ext({
    type: 'onRequest',
    method: function (request, h) {

        // Change all requests to '/test'

        request.setUrl('/test');
        return h.continue;
    }
});

server.route({ method: 'GET', path: '/test', handler: () => 'ok' });
await server.start();

// All requests will get routed to '/test'

await server.start();// doesn't work like that.

Maybe someone can fix that in manual?

Thanks,
Indrek

documentation

Most helpful comment

"The code examples are not really meant to be copied as-is" I understand You, but I don't agree with You. If examples are wrong what else could be wrong in that manual?

All 11 comments

Thanks for raising the issue, we're working on it: https://github.com/hapijs/hapi/pull/3684/

The code examples are not really meant to be copied as-is. If you don't know that you cannot call await outside of an async function, you probably are not experienced enough with async/await to use it. The docs assume you know how to use the language to begin with. I don't think that's an unreasonable assumption.

"The code examples are not really meant to be copied as-is" I understand You, but I don't agree with You. If examples are wrong what else could be wrong in that manual?

Examples are not wrong. They are just incomplete. And it's not a manual, it's a reference.

I am with @hueniverse on this. Once you get a basic understanding how async/await works, you wouldn't just copy the example code as-is and try to run it. For the minimum you will need to wrap the example in an async function, like this

(async () => {
  // example codes here
})()

So, I have been trying to get my feet wet with Hapi. I am with @indreek on this, sort of. Been with express for a while. As usual went to the source: https://hapijs.com/tutorials/getting-started?lang=en_US. The idea of a "tutorial" is to teach. With working examples. And how is a newbie, getting started, supposed to know the differences between hapi <16 & 17+ ? The tutorial does not tell you that there are breaking changes which will prevent it from working on hapi 17. When you take the time to add the line "It is compatible with x, why not say, not compatible with the current hapi that get when you do npm install --save hapi (which btw, I copied from the paragraph above on the same tutorial). I am for one, thinking of sticking with express though I like what hapi can get me. In my case, and I may have something else wrong even if, after much googling, I took out server.connection I still could not get the tutorial on said page to run without the following change.
async function start() {

try {
    await server.start();
}
catch (err) {
    console.log(err);
    process.exit(1);
}

console.log('Server running at:', server.info.uri);

};

start();

as opposed to:

server.start((err) => {

if (err) {
    throw err;
}
console.log(`Server running at: ${server.info.uri}`);

});

The documentation is extremely lagging. At least the getting started one should just work.

It will "just work" when it's updated to hapi 17. A person has to do that, in their free time. Lucky for you someone has kindly done it and it will be merged very soon. If you want to preview it, you can here: https://github.com/hapijs/hapijs.com/blob/990ffe80333d31561bfcf609797e86c35dd20198/lib/tutorials/en_US/getting-started.md

Reviewed that, thanks for sharing. It looks good. In my case, as I had pointed out, I am a newbie to hapi but not to node, so I was able to move ahead. My point was more along the lines of -documentation is lagging and that has consequences for uptake- especially when there are breaking changes. I understand that this is an open source project so people need to be able to do this in their "free time" but the "getting started" tutorials especially need to be good source of current info.

Just thought that I would share what I had come up with -FWIW:

const Hapi=require('hapi');

// Create a server with a host and port
const server=Hapi.server({
host:'localhost',
port:8000
});

// Add the route
server.route({
method:'GET',
path:'/',
handler:function(request,response) {

    return'hello world';
}

});

// Start the server
server.start().then(() => {
console.log(Server started at ${server.info.uri});
}).catch((err)=>{
console.log(err);
process.exit(1);
});

FWIW @vhcoder there is nothing wrong with the code in the documentation or the one you wrote. They are just different coding techniques. The example code uses await/async (and Promise) while your code uses Promise only.

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.

Was this page helpful?
0 / 5 - 0 ratings