forked from vvEii/NOTFLIX-midterm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
executable file
·103 lines (91 loc) · 3.02 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
98
99
100
101
102
103
/* eslint-disable camelcase */
// load .env data into process.env
require("dotenv").config();
// Web server config
const PORT = process.env.PORT || 8080;
const ENV = process.env.ENV || "development";
const express = require("express");
const bodyParser = require("body-parser");
const cookieSession = require("cookie-session");
const sass = require("node-sass-middleware");
const app = express();
const morgan = require("morgan");
// PG database client/connection setup
const { Pool } = require("pg");
const dbParams = require("./lib/db.js");
const db = new Pool(dbParams);
db.connect();
// Load the logger first so all (static) HTTP requests are logged to STDOUT
// 'dev' = Concise output colored by response status for development use.
// The :status token will be colored red for server error codes, yellow for client error codes, cyan for redirection codes, and uncolored for all other codes.
app.use(morgan("dev"));
app.set("view engine", "ejs");
app.use(cookieSession({ name: "session", keys: ["key1", "key2"] }));
app.use(bodyParser.urlencoded({ extended: true }));
app.use(
"/styles",
sass({
src: __dirname + "/styles",
dest: __dirname + "/public/styles",
debug: true,
outputStyle: "expanded",
})
);
app.use(express.static("public"));
// Note: mount other resources here, using the same pattern above
// item routes
const itemsRoutes = require("./routes/items");
// login routes
const loginRoutes = require("./routes/login");
// registeration routes
const registrationRoutes = require("./routes/registration");
// logout routes
const logoutRoutes = require("./routes/logout");
// add item routes
const addItemRoutes = require("./routes/addItem");
const deleteItem = require("./routes/deleteItem");
// categories routes
const categoriesRoutes = require('./routes/categories');
// favorite routes
const favoriteRoutes = require('./routes/favorite');
// cart route
const cartRoute = require("./routes/cart");
//submit order
const submitRoute = require("./routes/submit");
// review routes
const reviewRoutes = require('./routes/review');
// Mount all resource routes
app.use("/login", loginRoutes(db));
app.use("/register", registrationRoutes(db));
app.use("/logout", logoutRoutes(db));
app.use("/api/items", itemsRoutes(db));
app.use("/add", addItemRoutes(db));
app.use("/delete", deleteItem(db));
app.use('/categories', categoriesRoutes(db));
app.use('/favorite', favoriteRoutes(db));
app.use("/cart", cartRoute(db));
app.use("/submit", submitRoute(db));
app.use('/review', reviewRoutes(db));
// Home page
// Warning: avoid creating more routes in this file!
// Separate them into separate routes files (see above).
app.get("/", (req, res) => {
if (!req.session.user_info) {
templateVar = {
user: {
name: null,
is_admin: null
}
}
res.render("index", templateVar);
} else {
templateVar = {
user: req.session.user_info
}
res.render("index", templateVar);
}
res.render("index", user_info);
});
app.listen(PORT, () => {
console.log(`Example app listening on port ${PORT}`);
});