Getting started with hapijs

Other topics

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.

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 http://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 http://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);
});

Contributors

Topic Id: 3500

Example Ids: 12080,23358,24645

This site is not affiliated with any of the contributors.