-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
60 lines (45 loc) · 1.84 KB
/
index.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
const dotenv = require('dotenv');
dotenv.config({ path: `.env.${process.env.NODE_ENV || 'development'}` });
const express = require('express');
const bodyParser = require('body-parser');
const indexRoutes = require('./routes/index');
const authRoutes = require('./routes/auth');
const postRoutes = require('./routes/posts');
const { errorHandler } = require('./middlewares/error.middleware');
const cookieParser = require('cookie-parser');
const cors = require('cors'); // 引入 CORS 中間件
const logService = require('./services/log.service');
const app = express();
// 配置CORS (如果沒有設置Nginx,可以把註解的地方去掉,使用這個進行測試)
// 注意,若未設置Nginx,這個設置CSRF會驗證不通過
app.use(
cors({
// origin: 'http://localhost:4200', // 允許來自 Angular 客戶端的請求 (請讀者自行替換)
// methods: ['GET', 'POST', 'PUT', 'DELETE'], // 允許的 HTTP 方法
credentials: true, // 是否允許攜帶 cookie 等憑證
})
);
// middleware
app.use(bodyParser.json());
app.use(cookieParser(process.env.COOKIE_SECRET));
// 路由
app.use('/csrf', indexRoutes);
app.use('/auth', authRoutes); // 認證路由
app.use('/posts', postRoutes); // 保護的記錄路由
// 使用 Express 錯誤處理中間件
app.use(errorHandler);
// 全域錯誤處理(未捕捉的異常)
process.on('uncaughtException', (err) => {
logService.error(`未捕捉異常: ${err.message}`);
logService.error(`堆疊資訊: ${err.stack}`);
process.exit(1); // 終止應用程式
});
process.on('unhandledRejection', (reason, promise) => {
logService.error(`未捕捉的 Promise 拒絕: ${reason}`);
logService.error(`相關的 Promise: ${promise}`);
});
// 啟動服務器
const PORT = process.env.PORT || 4000;
app.listen(PORT, () => {
console.log(`伺服器正在執行於埠號 ${PORT}`);
});