-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
58 lines (48 loc) · 1.43 KB
/
app.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
const express = require("express");
const http = require("http");
const path = require("path");
const cors = require("cors");
const db = require('./db');
const bodyParser = require("body-parser");
const cookieParser = require("cookie-parser");
const { checkAuthCookie } = require("./middlewares/auth");
const Post = require("./models/mongoose").posts;
const normalizePort = require("normalize-port");
const app = express();
app.set("view engine", "ejs");
app.set("views", path.resolve("./views/"));
db.connect();
setMiddleware(app);
setAppRoutes(app);
const PORT = normalizePort(process.env.PORT || "3000");
http.createServer(app).listen(PORT, () => {
console.log(`Server started at port : ${PORT}`);
});
function setMiddleware(app) {
app.use(express.urlencoded({ extended: false }));
app.use(bodyParser.json({ limit: "1mb", extended: true }));
app.use(cookieParser());
app.use(cors());
app.use(checkAuthCookie("token"));
app.use(express.static(path.resolve("./public")));
/**
* Routes
*/
app.use("/post", require("./routes/post.routes"));
app.use("/user", require("./routes/user.routes"));
}
function setAppRoutes(app) {
app.get("/", async (req, res) => {
const allPosts = await Post.find({}).sort({ createdAt: 1 });
res.render("home", {
user: req.user,
posts: allPosts,
});
});
app.get("/about", (req, res) => {
res.render("about", {
user: req.user,
});
});
}
module.exports = app;