diff --git a/server/index.js b/server/index.js index a26142a..bdc5d0a 100644 --- a/server/index.js +++ b/server/index.js @@ -1,8 +1,8 @@ /* eslint-disable global-require */ const path = require('path'); -const Hapi = require('@hapi/hapi'); -const Inert = require('@hapi/inert'); +const Hapi = require('@auth0/hapi'); +const Inert = require('inert'); const jwt = require('hapi-auth-jwt2'); const config = require('../lib/config'); const logger = require('../lib/logger'); @@ -10,60 +10,65 @@ const routes = require('./routes'); const defaultHandlers = require('./handlers'); const auth = require('./auth'); -const createServer = async (handlers = defaultHandlers) => { - const server = Hapi.server({ +const createServer = (cb, handlers = defaultHandlers) => { + const server = new Hapi.Server(); + + server.connection({ host: 'localhost', port: config('PORT'), routes: { cors: true, + validate: {}, files: { relativeTo: path.join(__dirname, '../public') } } }); - // Register plugins - await server.register([jwt, Inert]); + server.register([jwt, Inert], () => {}); - // Define routes - server.route([ - { - method: 'GET', - path: '/js/{file*}', - options: { auth: false }, - handler: { - directory: { - path: path.join(__dirname, '../public/js') - } + server.route({ + method: 'GET', + path: '/js/{file*}', + config: { auth: false }, + handler: { + directory: { + path: path.join(__dirname, '../public/js') } - }, - { - method: 'GET', - path: '/css/{file*}', - options: { auth: false }, - handler: { - directory: { - path: path.join(__dirname, '../public/css') - } + } + }); + + server.route({ + method: 'GET', + path: '/css/{file*}', + config: { auth: false }, + handler: { + directory: { + path: path.join(__dirname, '../public/css') } } - ]); + }); + + server.register([auth, handlers, routes], (err) => { + // Use the server logger. + logger.debug = (...args) => { + server.log(['debug'], args.join(' ')); + }; + logger.info = (...args) => { + server.log(['info'], args.join(' ')); + }; + logger.error = (...args) => { + server.log(['error'], args.join(' ')); + }; - // Register additional plugins and routes - await server.register([auth, handlers, routes]); + if (err) { + cb(err); + } - // Configure server logging - logger.debug = (...args) => { - server.log(['debug'], args.join(' ')); - }; - logger.info = (...args) => { - server.log(['info'], args.join(' ')); - }; - logger.error = (...args) => { - server.log(['error'], args.join(' ')); - }; + cb(null, server); + }); - return server; // Return the server instance directly + return server; }; module.exports = createServer;