-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
62 lines (58 loc) · 1.5 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
const express = require("express")
const bodyParser = require("body-parser")
// const elasticsearch = require("elasticsearch")
const app = express()
app.use(bodyParser.json())
app.listen(process.env.PORT || 3000, () => {
console.log("connected")
})
// const esClient = elasticsearch.Client({
// host: "http://0.0.0.0:9200",
// })
const Client = require('@elastic/elasticsearch').Client;
const esClient = new Client({
// cloud: { id: '<cloud-id>' },
// auth: { apiKey: 'base64EncodedKey' }
cloud: { id: 'test' },
auth: {
username: 'elastic',
password: 'HKIYI6Ke7F2uhT4D4gPpEEiF'
}
})
app.get('/', (req, res) => {
res.send('hello world')
})
app.post("/products", (req, res) => {
esClient.index({
index: 'products',
body: {
"id": req.body.id,
"name": req.body.name,
"price": req.body.price,
"description": req.body.description,
}
})
.then(response => {
return res.json({"message": "Indexing successful"})
})
.catch(err => {
return res.status(500).json({"message": "Error"})
})
})
app.get("/products", (req, res) => {
const searchText = req.query.text
esClient.search({
index: "products",
body: {
query: {
match: {"name": searchText? searchText.trim(): ''}
}
}
})
.then(response => {
return res.json(response)
})
.catch(err => {
return res.status(500).json({"message": "Error"})
})
})