-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathserver.js
More file actions
95 lines (76 loc) · 2.65 KB
/
Copy pathserver.js
File metadata and controls
95 lines (76 loc) · 2.65 KB
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
const { MongoClient } = require("mongodb");
const cors = require("cors");
const express = require("express");
const app = express();
// or as an es module:
// import { MongoClient } from 'mongodb'
// Connection URL
const url = "mongodb://localhost:27017";
const client = new MongoClient(url);
// Database Name
const dbName = "editor";
// Use connect method to connect to the server
const connectDB = async (action, params) => {
await client.connect();
const db = client.db(dbName);
const collection = db.collection("editorCollection");
switch (action) {
case "get":
return await collection.find({}).toArray();
case "add":
return await collection.insertMany([params]);
case "edit":
return await collection.updateOne(params.old, { $set: params.update });
case "delete":
return await collection.deleteMany(params);
default:
break;
}
await client.close();
};
app.use(cors());
app.use(express.json()); // for parsing application/json
app.use(express.urlencoded({ extended: true })); // for parsing application/x-www-form-urlencoded
app.listen(5000);
app.use("/", express.static("ui"));
app.get("/list", (req, res) => {
connectDB("get")
.then((data) => res.end(JSON.stringify(data)))
.catch((error) => res.end(JSON.stringify(error)));
});
app.post("/add", (req, res) => {
connectDB("add", req.body)
.then((data) => res.end(JSON.stringify(data)))
.catch((error) => res.end(JSON.stringify(error)));
});
app.post("/edit", (req, res) => {
connectDB("edit", req.body)
.then((data) => res.end(JSON.stringify(data)))
.catch((error) => res.end(JSON.stringify(error)));
});
app.delete("/delete", (req, res) => {
connectDB("delete", req.body)
.then((data) => res.end(JSON.stringify(data)))
.catch((error) => res.end(JSON.stringify(error)));
});
// // the following code examples can be pasted here...
// const insertResult = await collection.insertMany([
// { a: 1 },
// { a: 2 },
// { a: 3 },
// ]);
// console.log("Inserted documents =>", insertResult);
// const filteredDocs = await collection.find({ a: 3 }).toArray();
// console.log("Found documents filtered by { a: 3 } =>", filteredDocs);
// const updateResult = await collection.updateOne({ a: 3 }, { $set: { b: 1 } });
// console.log("Updated documents =>", updateResult);
// const deleteResult = await collection.deleteMany({ a: 3 });
// console.log("Deleted documents =>", deleteResult);
// const indexName = await collection.createIndex({ a: 1 });
// console.log("index name =", indexName);
// return "done.";
// }
// main()
// .then(console.log)
// .catch(console.error)
// .finally(() => client.close());