-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
85 lines (70 loc) · 2.73 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
78
79
80
81
82
83
84
85
// Test -------------------------- Importing the Packages ---------------------------------
const express = require("express");
require("dotenv").config();
const db = require("./config/db");
const User = require("./models/userModel");
const app = express();
const port = 3000 || process.env.PORT;
// Test --------------------------- Middlewares -----------------------------------------
app.use(express.urlencoded({ extended: true }));
app.use(express.json());
// Test --------------------------- CREATE data ---------------------------------------
app.post("/", async (req, res) => {
const { userName, email, password } = req.body;
console.log(req.body);
try {
// Writing the Users in Users table (SQL Analogy)
// Able to write the duplicate data, need to do something of this
const newUser = await User.create({ userName, email, password });
res.json(newUser);
} catch (error) {
res.status(500).send(error);
}
})
// Test -------------------------- READ data ----------------------------------------
app.get("/", async (req, res, next) => {
try {
// Finding the users in the MongoDB Atlas
const users = await User.find();
res.json(users);
} catch (error) {
res.status(500).send(error);
}
});
// Test ------------------------- READ data - II ------------------------------------
app.get("/:id", async (req, res, next) => {
// Getting the parameters from the URL using req.params
const { id } = req.params;
try {
const findByIdUser = await User.findById(id);
res.json(findByIdUser);
} catch (error) {
res.status(500).send(error);
}
});
// Test -------------------------- UPDATE data -------------------------------------
app.put("/:id", async (req, res) => {
const { id } = req.params;
const { userName, email, password } = req.body;
try {
const updateUser = await User.findByIdAndUpdate(id, { userName, email, password });
res.json(updateUser);
} catch (error) {
res.status(500).send(error);
}
});
// Test -------------------------- DELETE data --------------------------------------
app.delete("/:id", async (req, res) => {
// Getting the parameters from the URL using req.params
const { id } = req.params;
try {
const deleteUser = await User.findByIdAndDelete(id);
res.send("Successfully deleted the data");
} catch (error) {
res.status(500).send(error);
}
})
// Test -------------------------- Error Handling for the server side code ---------------
app.listen(port, () => { console.log(`Server running on port ${port}`) });
// The middleware for handling errrors, is always at the last
app.use((error, req, res, next) => console.log(error));