X-Git-Url: https://git.r.bdr.sh/rbdr/generador-de-insultos/blobdiff_plain/67106a4644e19ed15a14d650d044a542961cdd62..e1c0668b66045505a69a227a7e2a175cb8ab35dd:/bin/server.js?ds=inline diff --git a/bin/server.js b/bin/server.js index 00c23bb..13ae381 100755 --- a/bin/server.js +++ b/bin/server.js @@ -2,33 +2,27 @@ 'use strict'; -var fs = require('fs'); -var path = require('path'); +const Fs = require('fs'); +const Path = require('path'); -var Hapi = require('hapi'); +const Hapi = require('@hapi/hapi'); -var server = new Hapi.Server(); -var InsultGenerator = require('../lib/InsultGenerator'); +const InsultGenerator = require('../lib/InsultGenerator'); -var insults = new InsultGenerator({ - file : path.resolve(__dirname, '../ext/words.tab') -}); -var replaceRe = /{{insult}}/ -var template; +const replaceRe = /{{insult}}/ +let template; -server.connection({ - host: 'localhost', - port: 9200 +const server = Hapi.Server({ + host: 'localhost', + port: 9200 }); server.route({ method: 'GET', path:'/', - handler: function (request, reply) { - insults.generate().then(function (insult) { - reply(template.replace(replaceRe, insult)); - }); + async handler(request, reply) { + return template.replace(replaceRe, await InsultGenerator.generate()); } }); @@ -42,15 +36,14 @@ server.route({ method: 'GET', path:'/raw', handler: function (request, reply) { - insults.generate().then(function (insult) { - reply(insult); - }); + + return InsultGenerator.generate(); } }); // Preload stuff, then run -console.log("Loading template..."); -fs.readFile(path.resolve(__dirname, '../ext/index.html'), {encoding: 'utf8'}, function (err, contents) { +console.debug('Loading template...'); +Fs.readFile(Path.resolve(__dirname, '../ext/index.html'), {encoding: 'utf8'}, function (err, contents) { if (err) { console.error(err); @@ -58,14 +51,19 @@ fs.readFile(path.resolve(__dirname, '../ext/index.html'), {encoding: 'utf8'}, fu } template = contents; - console.log("Setting up insults...") - insults.generate().then(function (insult) { + console.log('Setting up insults...') + InsultGenerator.generate() + .then(function (insult) { + server.start(function() { - console.log('Server running at:', server.info.uri); + + console.debug('Server running at:', server.info.uri); }); - }).catch(function (err) { + }) + .catch(function (err) { + console.error(err); - console.log("Error running insults... bad augur"); + console.error('Error running insults... bad augur'); process.exit(1); }); });