Hapi Example with skyapm

2020-05-10  本文已影响0人  AsaGuo
// main.js
// 在引入所有模块之前,启动skyapm-nodejs代理
require('skyapm-nodejs').start({
    serviceName: 'terminalapi2',
    directServers: '127.0.0.1:11800'
});

const Hapi = require('@hapi/hapi')
const userRoutes = require('./routes/users')
const HOST = process.env.HOST || 'localhost';
const PORT = process.env.PORT || '7000';

(async () => {
    const server = Hapi.server({
        host: HOST,
        port: PORT,

        // define route defaults
        routes: {
            // enable CORS
            cors: true,
            validate: {
                failAction: async (request, h, err) => {
                    throw Boom.badRequest(err.message);
                }
            }
        }
    })

    // definition of the base route
    server.route({
        method: 'GET',
        path: '/',
        handler: (request, h) => {
            // respond with a json object
            return h.response({ message: 'Asa is a super man!' })
        }
    })
    await server.register(userRoutes);
    server.start();
    console.info(`Server started at:${server.info.uri}`)
})();
上一篇下一篇

猜你喜欢

热点阅读