-
Notifications
You must be signed in to change notification settings - Fork 7
/
app.js
77 lines (61 loc) · 2.12 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
var express = require('express');
var path = require('path');
var logger = require('./logdna');
var elasticsearch = require('./elasticsearch');
const APP_PORT = 3000;
var index = require('./routes/index');
var suggest = require('./routes/suggest');
var search = require('./routes/stat');
var app = express();
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'pug');
// static folder setup
app.use(express.static(path.join(__dirname, 'public')));
elasticsearch.indexExists().then(
//delete index if it exist
function(status){
if(status){
return elasticsearch.deleteIndex();
}
}
).then(
function(){
logger.logger.log('Index deleted');
//create our index
return elasticsearch.createIndex().then(
function(){
logger.logger.log('Index created');
//Update our index with mappings
elasticsearch.indexMapping().then(
function(){
logger.logger.log('Index mapping has been updated');
//bulk add our dummy data in ./data/players.json
elasticsearch.bulkAddDocument().then(
function () {
logger.logger.log('Dummy documents have been bulk imported');
},
function (err) {
logger.logger.error('Could not import dummy documents', err);
}
)
},
function(err){
logger.logger.error('Could not create index', err);
}
)
},
function (err){
logger.logger.error('Could not create index', err);
}
);
}
);
// create our app endpoints
app.get('/', index);
app.get('/suggest/:text/:size', suggest);
app.get('/stat/:id/', search);
app.listen(APP_PORT, function(){
logger.logger.log('App is running');
console.log(`App is running on ${APP_PORT}`);
});