hapijs

Getting started with hapijs

Remarks#

This section provides an overview of what hapijs is, and why a developer might want to use it.

It should also mention any large subjects within hapijs, and link out to the related topics. Since the Documentation for hapijs is new, you may need to create initial versions of those related topics.

Versions#

VersionRelease NotesRelease Date
16https://github.com/hapijs/hapi/issues/33982016-11-29
15https://github.com/hapijs/hapi/issues/33232016-08-26
14https://github.com/hapijs/hapi/issues/32722016-07-29
13https://github.com/hapijs/hapi/issues/30402016-02-01
12https://github.com/hapijs/hapi/issues/29852016-01-04
11https://github.com/hapijs/hapi/issues/28502015-10-16
10https://github.com/hapijs/hapi/issues/27642015-09-11

Hello world

Create a server.js file with the following contents:

'use strict';

const Hapi = require('hapi');

// Create a server instance
const server = new Hapi.Server();

// Specify connections (server available on https://localhost:8000)
server.connection({ 
    port: 8000 
});

// Add a route
server.route({
    method: 'GET',
    path:'/hello', 
    handler: function (request, reply) {
        return reply('hello world');
    }
});

// Start the server
server.start((err) => {
    if (err) {
        throw err;
    }

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

Start Hapi.js Server

Run node server.js and open https://localhost:8000/hello in your browser.

Passing Parameters to a route

Parameters can be specified in path property of route configuration

'use strict';

const Hapi = require('hapi');

// Create a server with a host and port
const server = new Hapi.Server();

server.connection({ 
    host: 'localhost', 
    port: 8000 
});

// Add a route path with url param
server.route({
    method: 'GET',
    path:'/hello/{name}', 
    handler: function (request, reply) {
        // Passed parameter is accessible via "request.params" 
        return reply(`Hello ${request.params.name}`);
    }
});

// Start the server
server.start((err) => {
    if (err) {
        throw err;
    }
    console.log('Server running at:', server.info.uri);
});

Validation

'use strict';

const Hapi = require('hapi');
const Joi = require('joi');

// Create a server with a host and port
const server = new Hapi.Server();

server.connection({ 
    host: 'localhost', 
    port: 8000 
});

/**
 * Add a route path with url param
 */
server.route({
    method: 'GET',
    path:'/hello/{name}', 
    handler: function (request, reply) {
        // Passed parameter is accessible via "request.params" 
        return reply(`Hello ${request.params.name}`);
    },
    config: {
        // Validate the {name} url param
        validate: {
            params: Joi.string().required()
        }
    }
});

// Start the server
server.start((err) => {
    if (err) {
        throw err;
    }
    console.log('Server running at:', server.info.uri);
});

This modified text is an extract of the original Stack Overflow Documentation created by the contributors and released under CC BY-SA 3.0 This website is not affiliated with Stack Overflow