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