Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 'use strict'
- const Hapi = require('hapi')
- const fs = require('fs')
- let writeStream = fs.createWriteStream('output.log');
- async function start () {
- // Create a server with a host and port
- const server = Hapi.server({
- host: 'localhost',
- port: 3000
- })
- // Add the route
- server.route({
- method: 'GET',
- path: '/',
- handler: async function (request, h) {
- // request.log is HAPI standard way of logging
- request.log(['a', 'b'], 'Request into hello world')
- // a pino instance can also be used, which will be faster
- request.logger.info('In handler %s', request.path)
- return 'hello world'
- }
- })
- await server.register({
- plugin: require('hapi-pino'),
- options: {
- prettyPrint: process.env.NODE_ENV !== 'production',
- stream : writeStream
- }
- })
- // also as a decorated API
- server.logger().info('another way for accessing it')
- // and through Hapi standard logging system
- server.log(['subsystem'], 'third way for accessing it')
- await server.start()
- return server
- }
- start().catch((err) => {
- console.log(err)
- process.exit(1)
- })
Add Comment
Please, Sign In to add comment