-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
75 lines (67 loc) · 2.17 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
require("dotenv").config();
// required packages
const express = require("express");
const pg = require("pg");
const rowdy = require("rowdy-logger");
const cookieParser = require("cookie-parser");
const db = require("./models");
const cryptoJS = require("crypto-js");
const methodOverride = require("method-override");
const pool = new pg.Pool();
// app config
const PORT = process.env.PORT || 3333;
const app = express();
app.set("view engine", "ejs");
// middle wares
const rowdyRes = rowdy.begin(app);
app.use(require("express-ejs-layouts"));
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(methodOverride("_method"));
// DIY middleware
// happens on every request
app.use((req, res, next) => {
// handy dandy debugging request logger
// console.log(`[${new Date().toLocaleString()}] incoming request: ${req.method} ${req.url}`)
// console.log('request body:', req.body)
// modify the response to give data to the routes/middleware that is 'downstream'
res.locals.myData = "hi, I came from a middleware!";
// tell express that the middleware is done
next();
});
// auth middleware
app.use(async (req, res, next) => {
try {
// if there is a cookie --
if (req.cookies.userId) {
// try to find that user in the db
const userId = req.cookies.userId;
const decryptedId = cryptoJS.AES.decrypt(
userId,
process.env.ENC_KEY
).toString(cryptoJS.enc.Utf8);
const user = await db.user.findByPk(decryptedId);
// mount the found user on the res.locals so that later routes can access the logged in user
// any value on the res.locals is available to the layout.ejs
res.locals.user = user;
} else {
// the user is explicitly not logged in
res.locals.user = null;
}
next();
} catch (err) {
console.log(err);
}
});
// routes
app.get("/", async (req, res) => {
const allPost = await db.post.findAll();
res.render("index", { allpost: allPost });
});
// controllers
app.use("/users", require("./controllers/users"));
app.use("/post", require("./controllers/post"));
app.listen(PORT, () => {
console.log(`server is running on port ${PORT}`);
rowdyRes.print();
});