-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
102 lines (90 loc) · 2.81 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
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
const express = require("express");
const cookieParser = require("cookie-parser");
const morgan = require("morgan");
const path = require("path");
const session = require("express-session");
const bodyParser = require("body-parser");
const cors = require("cors");
const dotenv = require("dotenv");
const passport = require("passport");
const passportConfig = require("./passport");
const helmet = require("helmet");
const hpp = require("hpp");
const redis = require("redis");
const RedisStore = require("connect-redis").default;
const userRoutes = require("./routes/user");
const commentRoutes = require("./routes/comment");
const wikiRoutes = require("./routes/wiki");
const eventRoutes = require("./routes/event");
const mypageRoutes = require("./routes/user/mypage");
const pointRoutes = require("./routes/user/point");
dotenv.config();
const redisClient = redis.createClient({
url: `redis://${process.env.REDIS_USERNAME}:${process.env.REDIS_PASSWORD}@${process.env.REDIS_HOST}:${process.env.REDIS_PORT}/0`,
});
redisClient.on("connect", () => {
console.info(`Redis connected`);
});
redisClient.on("error", (err) => {
console.error(`Redis Client Error`, err);
});
redisClient.connect().then();
const userRoutes = require("./routes/user");
/* const commentRoutes = require("./routes/comment");
const wikiRoutes = require("./routes/wiki");
const eventRoutes = require("./routes/event"); */
const mypageRoutes = require("./routes/user/myPage");
dotenv.config();
const app = express();
passportConfig();
if (process.env.NODE_ENV === "production") {
app.use(morgan("combined"));
app.use(
helmet({
contentSecurityPolicy: false,
crossOriginEmbedderPolicy: false,
crossOriginResourcePolicy: false,
})
);
app.use(hpp());
} else {
app.use(morgan("dev"));
}
let corsOptions = {
origin: "http://localhost:3000",
credentials: true,
};
if (process.env.NODE_ENV === "production") {
corsOptions.origin = "http://asku.wiki";
}
app.use(cors(corsOptions));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(cookieParser(process.env.COOKIE_SECRET));
const sessionOption = {
resave: false,
saveUninitialized: false,
secret: process.env.COOKIE_SECRET,
cookie: {
httpOnly: true,
secure: false,
sameSite: "lax",
},
store: new RedisStore({ client: redisClient, prefix: "session: ", db: 0 }),
};
if (process.env.NODE_ENV === "production") {
sessionOption.proxy = true;
sessionOption.cookie.secure = true;
}
app.use(session(sessionOption));
app.use(passport.initialize());
app.use(passport.session());
app.use("/user", userRoutes);
/* app.use("/comment", commentRoutes);
app.use("/wiki", wikiRoutes);
app.use("/event", eventRoutes);
const port = process.env.PORT || 8080;
app.listen(port, () => {
console.log(`Server is running on port ${port}`);
});
module.exports = app;