-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
65 lines (57 loc) · 1.76 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
62
63
64
65
const express = require('express')
const app = express()
const cors = require('cors')
const {MongoClient, ObjectId } = require('mongodb')
const { response } = require('express')
const { request } = require('http')
require('dotenv').config()
const PORT = 8000
let db,
dbConnectionStr = process.env.DB_STRING,
dbName = 'sample_mflix',
collection
MongoClient.connect(dbConnectionStr)
.then(client => {
console.log(`Connected to database ${dbName}`)
db = client.db(dbName)
collection = db.collection(`movies`)
})
app.use(express.urlencoded({extended : true}))
app.use(express.json())
app.use(cors())
app.get("/search", async (request, response) => {
try {
let result = await collection.aggregate([
{
"$search" : {
"autocomplete" : {
"query": `${request.query.query}`,
"path": "title",
"fuzzy": {
"maxEdits": 2,
"prefixLength": 3
}
}
}
}
]).toArray()
// console.log(result)
response.send(result)
} catch (error) {
response.status(500).send({message: error.message})
// console.log(error)
}
})
app.get("/get/:id", async (request,response) => {
try {
let result = await collection.findOne({
"_id" : ObjectId(request.params.id)
})
response.send(result)
} catch (error) {
response.status(500).send({message: error.message})
}
})
app.listen(process.env.PORT || PORT, () => {
console.log(`Server is running on port ${PORT}`)
})