-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
73 lines (63 loc) · 1.93 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
66
67
68
69
70
71
72
73
const express = require("express");
const port = process.env.PORT || 5000;
const { MongoClient, ServerApiVersion, ObjectId } = require("mongodb");
const cors = require("cors");
require("dotenv").config();
const jwt = require("jsonwebtoken");
const app = express();
// middleware
app.use(cors());
app.use(express.json());
const uri = `mongodb+srv://${process.env.DB_USER}:${process.env.DB_PASSWORD}@cluster1.jggf1.mongodb.net/myFirstDatabase?retryWrites=true&w=majority`;
const client = new MongoClient(uri, {
useNewUrlParser: true,
useUnifiedTopology: true,
serverApi: ServerApiVersion.v1,
});
async function run() {
try {
client.connect();
const productCollection = client.db("EmaJohn").collection("products");
// Load products
app.get("/products", async (req, res) => {
const page = parseInt(req.query.page);
console.log(page);
const query = {};
const cursor = productCollection.find(query);
let products;
if (page) {
products = await cursor
.skip(page * 10)
.limit(10)
.toArray();
} else {
products = await cursor.limit(10).toArray();
}
res.send(products);
});
// Count Product
app.get("/count", async (req, res) => {
const count = await productCollection.estimatedDocumentCount();
res.send({ count });
});
// Setting Cart Product
app.post("/cartProducts", async (req, res) => {
const userId = req.body.userId;
const keys = req.body.keys;
const ids = keys.map((id) => ObjectId(id));
const query = { _id: { $in: ids } };
const cursor = productCollection.find(query);
const result = await cursor.toArray();
res.send(result);
});
} finally {
// client.close()
}
}
run().catch(console.dir);
app.get("/", (req, res) => {
res.send("Welcome to Ema John server");
});
app.listen(port, () => {
console.log("Server Running on: ", port);
});