Skip to content

Commit

Permalink
[#152] feat: 세션 적용 및 소켓 연결
Browse files Browse the repository at this point in the history
  • Loading branch information
domino8788 committed Dec 20, 2020
1 parent 705b3d7 commit d3b992c
Showing 1 changed file with 23 additions and 2 deletions.
25 changes: 23 additions & 2 deletions backend/src/App.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,21 @@
import express, { Application, Request, Response, NextFunction } from 'express';
import logger from 'morgan';
import createHttpError, { HttpError } from 'http-errors';
import session from 'express-session';
import cookieParser from 'cookie-parser';
import flash from 'connect-flash';
import { config } from 'dotenv';

import webSocket from '@/socket';
import { StatusCode, ErrorMessage } from '@/aops';
import { connect } from '@/models';
import { apiRouter } from '@/routes';
import { StatusCode, ErrorMessage } from '@/aops';

config();

export class App {
private app: Application;
private sessionMiddleware: any;

static bootstrap(port: number) {
const app = new App();
Expand All @@ -18,6 +26,15 @@ export class App {
connect();

this.app = express();
this.sessionMiddleware = session({
resave: false,
saveUninitialized: false,
secret: process.env.COOKIE_SECRET,
cookie: {
httpOnly: true,
secure: false,
},
});

this.initMiddlewares();
this.initRouters();
Expand All @@ -28,6 +45,9 @@ export class App {
this.app.use(logger('dev'));
this.app.use(express.json());
this.app.use(express.urlencoded({ extended: false }));
this.app.use(cookieParser(process.env.COOKIE_SECRET));
this.app.use(this.sessionMiddleware);
this.app.use(flash());
}

private initRouters() {
Expand All @@ -48,9 +68,10 @@ export class App {
}

private listen(port: number): void {
this.app
const server = this.app
.listen(port, () => console.log(`Express server listening at ${port}`))
.on('error', (err) => console.error(err));
webSocket(server, this.app, this.sessionMiddleware);
}
}

Expand Down

0 comments on commit d3b992c

Please sign in to comment.