Guest User

Untitled

a guest
Mar 16th, 2018
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.96 KB | None | 0 0
  1. import Hapi from 'hapi'
  2. const Good = require('good')
  3. const mysql = require('mysql2/promise');
  4.  
  5. var pool = mysql.createPool( {
  6. connectionLimit: 10,
  7. host: '127.0.0.1',
  8. user: 'root',
  9. password: '',
  10. database: 'demo'
  11. } );
  12.  
  13. const server = new Hapi.server({port: 3000})
  14.  
  15. server.route( {
  16. method: 'GET',
  17. path: '/',
  18. handler: async function( request, h ) {
  19. const [ rows, fields ] = await pool.query( 'select * from users limit 1' );
  20. return rows[ 0 ]
  21. }
  22. } )
  23.  
  24. async function start() {
  25.  
  26. try {
  27.  
  28. server.events.on( 'response', function( request ) {
  29. console.log(Date.now() + ': ' + request.info.remoteAddress + ': ' + request.method.toUpperCase() + ' ' + request.url.path + ' --> ' + request.response.statusCode );
  30. } );
  31.  
  32. // await server.register( {
  33. // plugin: Good,
  34. // } )
  35.  
  36. await server.start()
  37.  
  38. } catch ( err ) {
  39.  
  40. console.error( err )
  41. process.exit( 1 )
  42.  
  43. }
  44.  
  45. console.log( 'Server running at: ', server.info.uri )
  46.  
  47. }
  48.  
  49. start()
Add Comment
Please, Sign In to add comment