-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
71 lines (54 loc) · 1.6 KB
/
index.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
const express = require('express');
const bodyParser = require('body-parser')
const path = require('path');
const elasticsearch = require('elasticsearch');
const client = new elasticsearch.Client({
hosts: ['http://localhost:9200']
});
const app = express();
// ping the client to be sure Elasticsearch is up
client.ping({
requestTimeout: 30000,
}, function(error) {
if (error) {
console.error('elasticsearch cluster is down!');
} else {
console.log('Everything is ok');
}
});
app.use(bodyParser.json())
app.set('port', process.env.PORT || 3001);
app.use(express.static(path.join(__dirname, 'public')));
app.use(function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header('Access-Control-Allow-Methods', 'PUT, GET, POST, DELETE, OPTIONS');
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
next();
});
app.get('/', function(req, res) {
res.sendFile('template.html', {
root: path.join(__dirname, '../views')
});
})
app.get('/search', function(req, res) {
let body = {
size: 200,
from: 0,
query: {
match: {
name: req.query['q']
}
}
}
client.search({ index: 'scotch.io-tutorial', body: body, type: 'cities_list' })
.then(results => {
res.send(results.hits.hits);
})
.catch(err => {
console.log(err)
res.send([]);
});
})
app.listen(app.get('port'), function() {
console.log('Express server listening on port ' + app.get('port'));
});