-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathserver.js
More file actions
56 lines (49 loc) · 1.5 KB
/
server.js
File metadata and controls
56 lines (49 loc) · 1.5 KB
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
import express from "express";
import bodyParser from "body-parser";
import cors from "cors";
import session from "express-session";
import MongoStore from "connect-mongo";
// Route
import rootRouter from "./routes/rootRouter.js";
import userRouter from "./routes/userRouter.js";
import "./db.js";
import corsOptions from "./config/corsOptions.js";
import allowedOrigins from "./config/allowedOrigins.js";
import { localsMiddleware } from "./middlewares.js";
const app = express();
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
app.use(
session({
secret: "Hello!",
resave: false,
saveUninitialized: false,
store: MongoStore.create({
mongoUrl:
"mongodb+srv://yonghyun:47529722@diyparking.ddqzn68.mongodb.net/?retryWrites=true&w=majority",
}),
})
);
app.use(
cors({
origin: (origin, callback) => {
if (allowedOrigins.indexOf(origin) !== -1 || !origin) {
callback(null, true);
} else {
callback(new Error("Not allowed by CORS"));
}
}, // 출처 허용 옵션
credential: true, // 사용자 인증이 필요한 리소스(쿠키 ..등) 접근
})
);
app.use((req, res, next) => {
res.header("Access-Control-Allow-Credentials", true);
next();
});
app.use(localsMiddleware);
app.use("/", rootRouter);
app.use("/users", userRouter);
const PORT = process.env.PORT || 5000;
const handleListening = () =>
console.log(`✅ Server listenting on port http://localhost:${PORT} 🚀`);
app.listen(PORT, handleListening);