diff --git a/srv/data.js b/srv/data.js index 6f1f65c..62b4bd1 100644 --- a/srv/data.js +++ b/srv/data.js @@ -29,7 +29,7 @@ client.indices.create({ }); -const cities = require('./data/cities.json'); +const cities = require('../data/cities.json'); // declare an empty array called bulk var bulk = []; //loop through each city and create and push two objects into the array in each loop diff --git a/srv/index.js b/srv/index.js new file mode 100644 index 0000000..d7d8ee7 --- /dev/null +++ b/srv/index.js @@ -0,0 +1,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')); +}); \ No newline at end of file diff --git a/views/template.html b/views/template.html new file mode 100644 index 0000000..98a817f --- /dev/null +++ b/views/template.html @@ -0,0 +1,96 @@ + + + + +
lat:{{ result._source.lat }}, long: {{ result._source.lng }}.
+