-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.ts
75 lines (60 loc) · 1.76 KB
/
app.ts
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
import express, { NextFunction, Request, Response } from 'express';
import cors from 'cors';
import morgan from 'morgan';
import dotenv from 'dotenv';
import cookieParser from 'cookie-parser';
import passport from 'passport';
import * as redis from 'redis';
import authRouter from './routes/authRouter';
import { sequelize } from './models';
import todoRouter from './routes/todoRouter';
import passportConfig from './config/passport';
dotenv.config();
const app = express();
export const redisClient = redis.createClient({ legacyMode: true });
export const redisCli = redisClient.v4;
passportConfig();
const corOptions = {
origin: 'http://localhost:5173',
credentials: true,
};
redisClient.connect();
redisClient.on('connect', () => {
console.log('Connected to Redis');
});
redisClient.on('ready', () => {
console.log('Redis client is ready');
});
redisClient.on('error', (err) => {
console.error('Error connecting to Redis:', err);
});
redisClient.on('end', () => {
console.log('Disconnected from Redis');
});
sequelize
.sync({ force: false })
.then(() => {
console.log('데이터베이스 연결 성공');
})
.catch((err: Error) => {
console.error(err);
});
app.use(morgan('dev'));
app.use(cors(corOptions));
app.use(cookieParser());
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(passport.initialize());
app.use('/auth', authRouter);
app.use('/todos', passport.authenticate('jwt', { session: false }), todoRouter);
app.use((err: Error, req: Request, res: Response, next: NextFunction): void => {
console.error(err);
res.status(500).send({
message: 'Server Error',
error: err,
});
});
const PORT = process.env.PORT || 8080;
app.listen(PORT, () => {
console.log(`Server is running on Port ${PORT}!`);
});