-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
66 lines (61 loc) · 2.01 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
import dotenv from 'dotenv'
import express from 'express'
import passport from 'passport'
import morgan from 'morgan'
import db from './app/models/index.js'
import api from "./app/routes/api.js"
import basic from "./app/routes/basic.js"
import user from "./app/routes/user.js"
import index from "./app/routes/index.js"
import restaurant from "./app/routes/restaurant.js"
import getResponse from "./app/lambdas/getResponse.js"
import applyPassport from './app/lambdas/applyPassport.js'
import applyDotenv from './app/lambdas/applyDotenv.js'
import cors from 'cors'
async function startServer() {
const corsOptions = {
origin: 'http://localhost:3000',
optionsSuccessStatus: 200,
}
const app = express();
const { mongoUri, port, jwtSecret } = applyDotenv(dotenv)
app.use(cors(corsOptions))
app.use(express.static('public'));
app.use(express.urlencoded({ extended: true }));
app.use(express.json());
const _passport = applyPassport(passport, jwtSecret);
app.use(_passport.initialize());
app.use("/", index);
app.use("/api", api);
app.use("/basic", basic);
app.use("/restaurant", restaurant);
app.use("/user", user);
app.use(morgan('dev'))
db
.mongoose
.connect(mongoUri, {
useNewUrlParser: true,
useUnifiedTopology: true
})
.then(() => {
console.log(' ===== 몽고DB 연결 성공 ===== ')
})
.catch(err => {
console.log(' ===== 몽고DB와 연결 실패 =====', err)
process.exit();
});
app.all("*", function (_req, res) {
return getResponse.notFoundResponse(res, "페이지를 찾을 수 없습니다");
});
app.use((err, _req, res) => {
if (err.name == "UnauthorizedError") {
return getResponse.unauthorizedResponse(res, err.message);
}
});
app.listen(port, () => {
console.log('***************** ***************** *****************')
console.log('********** 서버가 정상적으로 실행되고 있습니다 *********')
console.log('***************** ***************** *****************')
})
}
startServer()