Introduzione ad Hapi.js

Mattepuffo's logo
Introduzione ad Hapi.js

Introduzione ad Hapi.js

Una delle cose che mi piace di più di Node.js, è che ci stanno tantissime librerie / framework disponibili!

Uno di questi, che trovo molto interessante, è Hapi.js!

Vediamo come creare un esempio per fare qualche test.

Create una directory, spostavei col terminale, e digitate questo:

$ npm install -save hapi

Questo creerà un file package.json.

Una cosa del genere:

{
  "name": "HapijsTest", "version": "1.0.0", "scripts": {
    "start": "node main.js"
}, "keywords": [
    "util", "functional", "server", "client", "browser"
  ], "author": "matte", "contributors": [], "dependencies": {
    "hapi": "^16.1.1"
  }
}

Quello che mi mancava era la voce scripts; impostatela per poter avviare il progetto.

Dentro al main.js mettete questo:

'use strict';

const Hapi = require('hapi');

const server = new Hapi.Server();
server.connection({port: 3000, host: 'localhost'});

server.route({
    method: 'GET',
    path: '/',
    handler: function (request, reply) {
        reply('Ciao da Hapi.js!');
    }
});

server.route({
    method: 'GET',
    path: '/{nome}',
    handler: function (request, reply) {
        reply('Ciao, ' + encodeURIComponent(request.params.nome) + '!');
    }
});

server.start((err) => {
    if (err) {
        throw err;
    }
    console.log(`Server inizializzato: ${server.info.uri}`);
});

Avviate il progetto; se tutto è ok avete due routes a disposizione:

  • http://localhost:3000
  • http://localhost:3000/VOSTRO_NOME_A_SCELTA

Adesso aggiungiamo la possibilità di "servire" file HTML statici; prima di tutto eseguite questo comando:

$ npm install --save inert

Abbiamo installato il modulo Inert per Hapi.js, che serve proprio per gestire file statici.

Adesso aggiungete un file HTML nella stessa directory, e poi modificate il file main.js:

'use strict';

const Hapi = require('hapi');

const server = new Hapi.Server();
server.connection({port: 3000, host: 'localhost'});

server.route({
    method: 'GET',
    path: '/',
    handler: function (request, reply) {
        reply('Ciao da Hapi.js!');
    }
});

server.route({
    method: 'GET',
    path: '/{nome}',
    handler: function (request, reply) {
        reply('Ciao, ' + encodeURIComponent(request.params.nome) + '!');
    }
});

server.register(require('inert'), (err) => {
    if (err) {
        throw err;
    }

    server.route({
        method: 'GET',
        path: '/static',
        handler: function (request, reply) {
            reply.file('./ciao.html');
        }
    });
});

server.start((err) => {
    if (err) {
        throw err;
    }
    console.log(`Server inizializzato: ${server.info.uri}`);
});

Abbiamo aggiunto una route, in cui richiamiamo inert.

Riavviate il programma, a provate ad andare sulla route appena aggiunta.

Hapi.js ha anche altri plugins da installare; li potete trovare nella pagina apposita.

Se vi piace Node.js, vi consiglio di provare questo framework!

Enjoy!


Condividi

Commentami!