이정민

app.js

Showing 1 changed file with 69 additions and 0 deletions
1 +const express = require('express');
2 +const path = require('path');
3 +const morgan = require('morgan');
4 +const cookieParser = require('cookie-parser');
5 +const session = require('express-session');
6 +const flash = require('connect-flash');
7 +const passport = require('passport');
8 +require('dotenv').config();
9 +
10 +const indexRouter = require('./routes/index');
11 +const authRouter = require('./routes/auth');
12 +const { sequelize } = require('./models');
13 +const passportConfig = require('./passport');
14 +const sse = require('./sse');
15 +const webSocket = require('./socket');
16 +const checkAuction = require('./checkAuction');
17 +
18 +const app = express();
19 +sequelize.sync();
20 +passportConfig(passport);
21 +checkAuction();
22 +
23 +const sessionMiddleware = session({
24 + resave: false,
25 + saveUninitialized: false,
26 + secret: process.env.COOKIE_SECRET,
27 + cookie: {
28 + httpOnly: true,
29 + secure: false,
30 + },
31 +});
32 +
33 +app.set('views', path.join(__dirname, 'views'));
34 +app.set('view engine', 'pug');
35 +app.set('port', process.env.PORT || 8010);
36 +
37 +app.use(morgan('dev'));
38 +app.use(express.static(path.join(__dirname, 'public')));
39 +app.use('/img', express.static(path.join(__dirname, 'uploads')));
40 +app.use(express.json());
41 +app.use(express.urlencoded({ extended: false }));
42 +app.use(cookieParser(process.env.COOKIE_SECRET));
43 +app.use(sessionMiddleware);
44 +app.use(passport.initialize());
45 +app.use(passport.session());
46 +app.use(flash());
47 +
48 +app.use('/', indexRouter);
49 +app.use('/auth', authRouter);
50 +
51 +app.use((req, res, next) => {
52 + const err = new Error('Not Found');
53 + err.status = 404;
54 + next(err);
55 +});
56 +
57 +app.use((err, req, res, next) => {
58 + res.locals.message = err.message;
59 + res.locals.error = req.app.get('env') === 'development' ? err : {};
60 + res.status(err.status || 500);
61 + res.render('error');
62 +});
63 +
64 +const server = app.listen(app.get('port'), () => {
65 + console.log(app.get('port'), '번 포트에서 대기중');
66 +});
67 +
68 +webSocket(server, app);
69 +sse(server);