-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
48 lines (41 loc) · 1.23 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
require('dotenv').config();
const express = require('express');
const bodyParser = require('body-parser');
const { MongoClient, ServerApiVersion } = require('mongodb');
// Replace with your MongoDB Atlas connection string
const uri = process.env.MONGO_URI;
const app = express();
const port = process.env.PORT || 5000;
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
const client = new MongoClient(uri, {
serverApi: {
version: ServerApiVersion.v1,
strict: true,
deprecationErrors: true,
}
});
app.post('/knowitall', async (req, res) => {
const query = req.body.query;
try{
await client.connect();
console.log("Successfully connected to mongoDB!")
} finally {
const db = client.db('Pages').collection('Pages');
if((result = await db.findOne({
title:query
})) != null){
// console.log(result)
await client.close()
res.status(200).send({
data:result
});
}
}
// Handle data request logic here
// res.send('<h1>Hello World</h1>');
res.status(404).send()
});
app.listen(port, () => {
console.log(`Server is running on port ${port}`);
});