-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
62 lines (59 loc) · 1.5 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
const express = require("express");
const mongoose = require("mongoose");
const {
render
} = require("ejs");
require("dotenv/config");
const app = express();
app.set("view engine", "ejs");
app.use(express.static("public"));
// //Import Route
// const authRoute = require("./router/auth");
const pageRoute = require("./routes/page");
//MODEL
const User = require("./model/User");
mongoose.createConnection(
process.env.DB_CONNECTION_ONLINE, {
useNewUrlParser: true,
useUnifiedTopology: true
},
() => {
console.log("Connected to DB.");
}
);
app.get("/", (req, res) => res.status(200).render("index"));
app.get("/deleteAll", (req, res) => {
User.find()
.then((data) => {
data.forEach((user) => {
User.findByIdAndDelete(`${user._id}`)
.then(() => {
console.log(`User " ${user.name} " deleted successfully`);
})
.catch(() => {
console.log(`Unable to delete user :" ${user.name} "`);
});
});
res.status(201).json(data);
})
.catch((e) => {
console.log(e);
});
});
app.get("/getAll", (req, res) => {
User.find()
.then((data) => {
res.status(200).send(data);
})
.catch((e) => {
console.log(e);
});
});
// //Middlewares
// // app.use(express.json());
// app.use(express.urlencoded({ extended: true }));
//Route Middleware
// app.use("/", authRoute);
app.use("/page", pageRoute);
const PORT = process.env.PORT || 3000;
app.listen(PORT, () => console.log("Server is UP."));