-
Notifications
You must be signed in to change notification settings - Fork 0
/
mongoCoonectionAndCRUD.js
156 lines (138 loc) · 4.07 KB
/
mongoCoonectionAndCRUD.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
const express = require("express");
const fs = require("fs");
const mongoose = require("mongoose");
const users = require("./Data/MOCK_DATA.json");
const app = express();
const PORT = 1010;
//connecting mongo
// Connection string with optional Mongoose options
const connectionString = "mongodb://localhost:27017/rest-app-test";
mongoose
.connect(connectionString)
.then(() => console.log("MongoDB connected"))
.catch((err) => console.error("Mongo error:", err));
const userSchema = new mongoose.Schema(
{
firstName: {
type: String,
required: true,
},
lastName: {
type: String,
},
email: {
type: String,
required: true,
unique: true,
},
jobTitle: {
type: String,
},
gender: {
type: String,
},
},
{ timestamps: true }
);
const User = mongoose.model("user", userSchema);
//MiddleWare : here are a functions which
app.use(express.urlencoded({ extended: false }));
// middleware have access of request object , res object , next middleware function
app.use((req, res, next) => {
console.log("Hello from the middleware 1");
// return res.end("Hello from middleware 2"); // this reurns and ends the req cycle before forwarding it to next middleware or next avaliable functions
next(); // this sends to the next middleware or next fucntion in the stack , if we dont use this the request won't be forwarded it will hang up in between
});
app.use((req, res, next) => {
req.name = "Anand"; // as using middleware we can manupulate the req data , that data will get avaliable to the next middlewares and the functions
//like we can first authenticate the user and then we can forward it or the important data of its get sent to the next of the functions if that user is not valid we can end that request
next();
});
app.use((req, res, next) => {
fs.appendFile(
"./Data/log.txt",
`${Date.now()} : ${req.method} : ${req.path} \n`,
(err, data) => {}
);
next();
});
// routes
app.get("/users", async (req, res) => {
/**
* we will return HTML so the structure will be like
* <ul> <li> ${username} </li></ul>
*/
const allDBUsers = await User.find({});
const HTML = `
<ul>
${allDBUsers
.map((user) => `<li>${user.firstName} - ${user.email}</li>`)
.join("")}
</ul>
`;
return res.send(HTML);
});
app.get("/api/users", async (req, res) => {
const allDBuders = await User.find({});
return res.json(allDBuders);
});
// grouping the same route requests
app
.route("/api/users/:id")
.get(async (req, res) => {
const user = await User.findById(req.params.id);
if (req.params.id > User.length) {
res.status(404).send({ msg: "Please enter valid user id" });
}
return res.json(user);
})
.patch(async (req, res) => {
//TODO : edit the user with id
const body = req.body;
console.log(body, "body in patch");
await User.findByIdAndUpdate(req.params.id, {
lastName: body.last_name,
});
return res.status(202).json({ msg: "Success" });
})
.delete(async (req, res) => {
//TODO : delete the user with id
await User.findByIdAndDelete(req.params.id);
return res.status(202).json({ msg: "Success" });
});
app.post("/api/users", async (req, res) => {
//TODO : create new user
const body = req.body;
if (
!body ||
!body.first_name ||
!body.last_name ||
!body.job_title ||
!body.email ||
!body.gender
) {
res.status(400).send({ msg: "please enter full data" });
}
const result = await User.create({
firstName: body.first_name,
lastName: body.last_name,
email: body.email,
gender: body.gender,
jobTitle: body.job_title,
});
console.log(result, "result");
return res.status(201).json({
msg: "success",
});
// console.log("REQ Body", body);
// users.push({
// id: users.length + 1,
// ...body,
// });
// fs.writeFile("./Data/MOCK_DATA.json", JSON.stringify(users), (err, data) => {
// return res.status(201).json({ status: "Success", id: users.length });
// });
});
app.listen(PORT, () => {
console.log(`Server started and running on PORT : ${PORT}`);
});