-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathindex.js
70 lines (62 loc) · 1.74 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
require("dotenv").config();
const express = require("express");
const adminRoutes = require("./routes/admin");
const shopRoutes = require("./routes/shop");
const PageNotFound = require("./helper/NotFound");
const bodyParser = require("body-parser");
const path = require("path");
// const mongoConnect = require("./helper/database").mongoConnect;
const User = require("./models/user");
const mongoose = require("mongoose");
const mongo_DB_URI = process.env.MONGO_DB_URI;
const app = express();
const PORT = 3000;
// app.set("view engine", "pug");
app.set("view engine", "ejs");
app.set("views", "views");
// const rootDir = require("./helper/path");
app.use(express.static(path.join(__dirname, "public")));
app.use(bodyParser.urlencoded({ extended: false }));
app.use((req, res, next) => {
User.findById("65aaa79a8945687d161ef472")
.then((user) => {
req.user = user;
next();
})
.catch((err) => {
console.log(err);
});
});
app.use("/admin", adminRoutes);
app.use(shopRoutes);
app.use(PageNotFound);
mongoose
.connect(mongo_DB_URI)
.then((result) => {
app.listen(PORT, () => {
console.log("Database connected");
User.findOne().then((user) => {
if (!user) {
const user = new User({
name: "David",
email: "[email protected]",
cart: {
items: [],
},
});
user.save();
} else {
console.log("User Already Exist");
}
});
console.log("App is running on the port http://localhost:3000");
});
})
.catch((err) => {
console.log(err);
});
// mongoConnect(() => {
// app.listen(PORT, () => {
// console.log("App is running on the port http://localhost:3000");
// });
// });