]> git.r.bdr.sh - rbdr/generador-de-insultos/blame - bin/server.js
Update README
[rbdr/generador-de-insultos] / bin / server.js
CommitLineData
1a652aac
BB
1#!/usr/bin/env node
2
3'use strict';
4
5var fs = require('fs');
6var path = require('path');
7
8var Hapi = require('hapi');
9
10var server = new Hapi.Server();
11var InsultGenerator = require('../lib/InsultGenerator');
12
13var insults = new InsultGenerator({
14 file : path.resolve(__dirname, '../ext/words.tab')
15});
16var replaceRe = /{{insult}}/
17var template;
18
19
20server.connection({
21 host: 'localhost',
22 port: 9200
23});
24
25server.route({
26 method: 'GET',
27 path:'/',
28 handler: function (request, reply) {
29 insults.generate().then(function (insult) {
30 reply(template.replace(replaceRe, insult));
31 });
32 }
33});
34
4fc8123b 35server.route({
9002702b
BB
36 config: {
37 cors: {
38 origin: ['*'],
39 additionalHeaders: ['cache-control', 'x-requested-with']
40 }
41 },
4fc8123b
BB
42 method: 'GET',
43 path:'/raw',
44 handler: function (request, reply) {
45 insults.generate().then(function (insult) {
46 reply(insult);
47 });
48 }
49});
50
1a652aac
BB
51// Preload stuff, then run
52console.log("Loading template...");
53fs.readFile(path.resolve(__dirname, '../ext/index.html'), {encoding: 'utf8'}, function (err, contents) {
54
55 if (err) {
56 console.error(err);
57 process.exit(1)
58 }
59
60 template = contents;
61 console.log("Setting up insults...")
62 insults.generate().then(function (insult) {
63 server.start(function() {
64 console.log('Server running at:', server.info.uri);
65 });
66 }).catch(function (err) {
67 console.error(err);
68 console.log("Error running insults... bad augur");
69 process.exit(1);
70 });
71});