-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
97 lines (83 loc) · 2.87 KB
/
server.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
const express = require("express");
const dotenv = require("dotenv");
const morgan = require("morgan");
const bodyparser = require("body-parser");
const path = require("path");
const expressLayouts = require("express-ejs-layouts");
const methodOverride = require("method-override");
const cookieParser = require("cookie-parser");
const app = express();
dotenv.config({ path: "config.env" });
const PORT = process.env.PORT || 8080;
//log requests
app.use(morgan("tiny"));
//bodyParser
app.use(bodyparser.urlencoded({ limit: "10mb", extended: true }));
app.use(cookieParser());
// routes
const indexRouter = require("./routes/index");
const offerRouter = require("./routes/offers");
const itemsRouter = require("./routes/items-router");
const usersRouter = require("./routes/users");
const staffRouter = require("./routes/staffs");
const interventionRouter = require("./routes/Interventions");
const carRouter = require("./routes/cars");
const shopRouter = require("./routes/shop");
const authentificationRouter = require("./routes/userAuth");
//view engine
app.set("view engine", "ejs");
app.set("views", __dirname + "/views");
app.set("layout", "layouts/layout");
app.use(expressLayouts);
app.use(methodOverride("_method"));
//load assets
app.use("/css", express.static(path.resolve(__dirname, "assets/css")));
app.use("/img", express.static(path.resolve(__dirname, "assets/img")));
app.use("/js", express.static(path.resolve(__dirname, "assets/js")));
// public folder
app.use(express.static("public"));
app.use(methodOverride("_method"));
// mongoose
const mongoose = require("mongoose");
const { nextTick } = require("process");
mongoose.connect(process.env.DATABASE_URL, {
useNewUrlParser: true,
useUnifiedTopology: true,
});
const db = mongoose.connection;
db.on("error", (error) => console.error(error));
db.once("open", () => console.log("Connected to Mongoose"));
// Initializing cart local storage
if (typeof localStorage === "undefined" || localStorage === null) {
var LocalStorage = require("node-localstorage").LocalStorage;
localStorage = new LocalStorage("./scratch");
}
const cart = {
items: [],
offers: [],
};
localStorage.setItem("cart", JSON.stringify(cart));
// using routes
app.use("*", (req, res, next) => {
const cart = JSON.parse(localStorage.getItem("cart"));
const totalItems = cart.offers.length + cart.items.length;
let isConnected = false;
if (req.cookies.token) {
isConnected = true;
}
res.locals.totalItems = totalItems;
res.locals.isConnected = isConnected;
next();
});
app.use("/", indexRouter);
app.use("/offers", offerRouter);
app.use("/items", itemsRouter);
app.use("/api/users", usersRouter);
app.use("/api/staff", staffRouter);
app.use("/interventions", interventionRouter);
app.use("/cars", carRouter);
app.use("/shop", shopRouter);
app.use("/auth", authentificationRouter);
app.listen(PORT, () => {
console.log("server is running on " + PORT);
});