-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
77 lines (75 loc) · 2.26 KB
/
index.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
66
67
68
69
70
71
72
73
74
75
76
77
const express = require("express");
const app = express();
const port = process.env.PORT || 5000;
const cors = require("cors");
const { MongoClient, ServerApiVersion } = require("mongodb");
const ObjectId = require("mongodb").ObjectId;
app.use(cors());
app.use(express.json());
const uri =
"mongodb+srv://dbusername01:PwoyUhdkoLH4EGnR@cluster0.dbx4u.mongodb.net/myFirstDatabase?retryWrites=true&w=majority";
const client = new MongoClient(uri, {
useNewUrlParser: true,
useUnifiedTopology: true,
serverApi: ServerApiVersion.v1,
});
async function run() {
try {
await client.connect();
const userCollection = client.db("foodExpress").collection("user");
// SINGLE USER DETAILS
app.get("/user/:id", async (req, res) => {
id = req.params.id;
const query = { _id: ObjectId(id) };
const result = await userCollection.findOne(query);
res.send(result);
});
// GET USER
app.get("/user", async (req, res) => {
const query = {};
const cursor = userCollection.find(query);
const user = await cursor.toArray();
res.send(user);
});
// POST USER
app.post("/user", async (req, res) => {
const user = req.body;
console.log(user);
const result = await userCollection.insertOne(user);
res.send(result);
});
// UPDATE USER
app.put("/user/:id", async (req, res) => {
const id = req.params.id;
const updateUser = req.body;
const filter = { _id: ObjectId(id) };
const options = { upsert: true };
const updateDoc = {
// $set: {
// // name: updateUser.name,
// // email: updateUser.email
// updateUser,
// },
$set: updateUser,
};
const result = await userCollection.updateOne(filter, updateDoc, options);
res.send(result);
});
// DELETE USER
app.delete("/user/:id", async (req, res) => {
const id = req.params.id;
const query = { _id: ObjectId(id) };
const result = await userCollection.deleteOne(query);
res.send(result);
});
} finally {
// await client.close()
}
}
run().catch(console.dir);
app.get("/", (req, res) => {
res.send("Mongo Db first website");
});
app.listen(port, () => {
console.log(`Example app listening on port ${port}`);
});