Guest User

Untitled

a guest
Dec 10th, 2018
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.09 KB | None | 0 0
  1. 'use strict'
  2.  
  3. const Hapi = require('hapi')
  4. const fs = require('fs')
  5. let writeStream = fs.createWriteStream('output.log');
  6.  
  7. async function start () {
  8. // Create a server with a host and port
  9. const server = Hapi.server({
  10. host: 'localhost',
  11. port: 3000
  12. })
  13.  
  14. // Add the route
  15. server.route({
  16. method: 'GET',
  17. path: '/',
  18. handler: async function (request, h) {
  19. // request.log is HAPI standard way of logging
  20. request.log(['a', 'b'], 'Request into hello world')
  21.  
  22. // a pino instance can also be used, which will be faster
  23. request.logger.info('In handler %s', request.path)
  24.  
  25. return 'hello world'
  26. }
  27. })
  28.  
  29. await server.register({
  30. plugin: require('hapi-pino'),
  31. options: {
  32. prettyPrint: process.env.NODE_ENV !== 'production',
  33. stream : writeStream
  34. }
  35. })
  36.  
  37. // also as a decorated API
  38. server.logger().info('another way for accessing it')
  39.  
  40. // and through Hapi standard logging system
  41. server.log(['subsystem'], 'third way for accessing it')
  42.  
  43. await server.start()
  44.  
  45. return server
  46. }
  47.  
  48. start().catch((err) => {
  49. console.log(err)
  50. process.exit(1)
  51. })
Add Comment
Please, Sign In to add comment