-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.js
61 lines (51 loc) · 1.61 KB
/
server.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
var express = require('express');
var elasticsearch = require('elasticsearch');
var fs = require('fs');
//create an instance of the elasticsearch.Client class
var client = new elasticsearch.Client({
host: 'localhost:9200',
log: 'trace'
});
//check Elastic node availability
client.ping({
requestTimeout: 30000
}, function (error) {
if (error) {
console.error('elasticsearch cluster is down!');
} else {
console.log('All is well');
}
});
//variable to store data from index
var dataFromElastic = null;
//search the index for required document
client.search({
index: 'testbase',
type: 'dataset'
}).then(function (resp) {
//get the document from node's response
var hits = resp.hits.hits;
dataFromElastic = hits[0]._source.data;
console.log(dataFromElastic);
}, function (err) {
console.trace(err.message);
});
// Creates server instance
var app = express();
//create the HTML page and send to the host
app.get('/', function (req, res) {
if (dataFromElastic) {
// For this demo we are using a fs.readFileSync and string replace methods to render the page,
// in real world application you might use one of the dozens Node.js templating engines.
var chartTemplate = fs.readFileSync(__dirname + '/index.html').toString();
var page = chartTemplate.replace("'{{data}}'", JSON.stringify(dataFromElastic));
res.send(page);
} else {
res.status(404);
res.send('Data not found');
}
});
// Runs express server
app.listen(8080, function () {
console.log('Example app is listening on port ' + 8080 + '!\n');
});