-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.js
58 lines (52 loc) · 1.77 KB
/
main.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
const { MongoClient, ObjectID } = require("mongodb");
const Express = require("express");
const Cors = require("cors");
const BodyParser = require("body-parser");
const { request } = require("express");
// const client = new MongoClient(process.env["ATLAS_URI"]);
const client = new MongoClient("mongodb+srv://demo_user:[email protected]/myFirstDatabase?retryWrites=true&w=majority");
const server = Express();
server.use(BodyParser.json());
server.use(BodyParser.urlencoded({ extended: true }));
server.use(Cors());
var collection;
var port = process.env.PORT || 4000;
server.get("/search", async (request, response) => {
try {
let result = await collection.aggregate([
{
"$search": {
"autocomplete": {
"query": `${request.query.query}`,
"path": "name",
"fuzzy": {
"maxEdits": 2,
"prefixLength": 3
}
}
}
}
]).toArray();
response.send(result);
} catch (e) {
response.status(500).send({ message: e.message });
}
});
server.get("/get/:id", async (request, response) => {
try {
let result = await collection.findOne({ "_id": ObjectID(request.params.id) });
response.send(result);
} catch (e) {
response.status(500).send({ message: e.message });
}
});
server.listen(port, async () => {
try {
await client.connect();
// collection = client.db("zynadit").collection("items");
collection = client.db("food").collection("recipes");
} catch (e) {
console.error(e);
}
console.log('Magic happens on port: ' + port);
});