Hello World

使用以下內容建立 server.js 檔案:

'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);
});

啟動 Hapi.js 伺服器

執行 node server.js 並在瀏覽器中開啟 http:// localhost:8000 / hello