forked from UMCreadme/Read_Me_Node.js
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
99 lines (75 loc) Β· 3.15 KB
/
Copy pathindex.js
File metadata and controls
99 lines (75 loc) Β· 3.15 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
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
import express from 'express';
import http from 'http';
import { pool } from './config/db.config.js';
import { response } from './config/response.js';
import { BaseError } from './config/error.js';
import { status } from './config/response.status.js';
import dotenv from 'dotenv';
import cron from 'node-cron';
import cors from 'cors';
import { testRouter } from './src/test/test.route.js';
import { userRouter } from './src/users/users.route.js';
import { shortsRouter } from './src/shorts/shorts.route.js';
import { homeRouter } from './src/home/home.route.js';
import { bookRouter } from './src/book/book.route.js';
import { communitiesRouter } from './src/communities/communities.route.js';
import { researchRouter } from './src/research/research.route.js';
import { healthRouter } from './src/health/health.route.js';
import { chatRouter } from './src/chat/chat.route.js';
import initializeSocket from './config/socketServer.js'; // 'initializeSocket' ν¨μλ₯Ό defaultλ‘ μν¬νΈ
dotenv.config();
const app = express();
const server = http.createServer(app); // HTTP μλ² μμ±
// μλ² μ€μ
app.set('port', process.env.PORT || 3000);
app.use(cors());
app.use(express.static('public'));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
// λΌμ°ν° μ€μ
app.use('/test', testRouter);
app.use('/users', userRouter);
app.use('/shorts', shortsRouter);
app.use('/home', homeRouter);
app.use('/books', bookRouter);
app.use('/communities', communitiesRouter);
app.use('/communities', chatRouter);
app.use('/recent-searches', researchRouter);
app.use('/health', healthRouter);
// μμΌ μ΄κΈ°ν
const io = initializeSocket(server); // μμΌ μλ² μ΄κΈ°ν
app.use((req, res, next) => {
console.log(`Requested URL: ${req.originalUrl}`);
const err = new BaseError(status.NOT_FOUND);
next(err);
});
// error handling
app.use((err, req, res, next) => {
console.log(err);
res.locals.message = err.message;
res.locals.error = process.env.NODE_ENV !== 'production' ? err : {};
if (err instanceof BaseError) {
return res.status(err.data.status).send(response(err.data));
} else {
return res.send(response(status.INTERNAL_SERVER_ERROR));
}
});
// μλ² μμ
server.listen(app.get('port'), () => {
console.log(`Server is running on port ${app.get('port')}`);
});
cron.schedule('0 0 * * *', async () => {
try {
const twoWeeksAgo = new Date();
twoWeeksAgo.setDate(twoWeeksAgo.getDate() - 14);
const formattedDate = twoWeeksAgo.toISOString().slice(0, 19).replace('T', ' '); // YYYY-MM-DD HH:MM:SS νμ
const conn = await pool.getConnection();
const query = 'UPDATE COMMUNITY SET is_deleted = 1 WHERE meeting_date <= ? AND is_deleted = 0';
const [results] = await conn.query(query, [formattedDate]);
console.log(`Updated ${results.affectedRows} records.`);
conn.release();
} catch(err) {
console.error('Error executing cron job:', err); // μλ¬ λ‘κ·Έ μΆκ°
throw new BaseError(status.INTERNAL_SERVER_ERROR);
}
});