Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import Hapi from 'hapi'
- const Good = require('good')
- const mysql = require('mysql2/promise');
- var pool = mysql.createPool( {
- connectionLimit: 10,
- host: '127.0.0.1',
- user: 'root',
- password: '',
- database: 'demo'
- } );
- const server = new Hapi.server({port: 3000})
- server.route( {
- method: 'GET',
- path: '/',
- handler: async function( request, h ) {
- const [ rows, fields ] = await pool.query( 'select * from users limit 1' );
- return rows[ 0 ]
- }
- } )
- async function start() {
- try {
- server.events.on( 'response', function( request ) {
- console.log(Date.now() + ': ' + request.info.remoteAddress + ': ' + request.method.toUpperCase() + ' ' + request.url.path + ' --> ' + request.response.statusCode );
- } );
- // await server.register( {
- // plugin: Good,
- // } )
- await server.start()
- } catch ( err ) {
- console.error( err )
- process.exit( 1 )
- }
- console.log( 'Server running at: ', server.info.uri )
- }
- start()
Add Comment
Please, Sign In to add comment