驗證

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