-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
67 lines (54 loc) · 1.92 KB
/
app.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
61
62
63
64
65
66
67
const express = require('express');
const { getExpressSessionStore } = require('./config/session-config');
const mongoose = require('mongoose');
const passport = require('passport');
const { createServer } = require('http');
const morgan = require('morgan');
const cors = require('cors');
const flash = require('connect-flash');
require('dotenv').config();
const app = express();
// Express Incoming data Body Parser
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
//Set up EJS
app.set('view-engine', 'ejs');
app.use(express.static('public'));
//Adding view layer helper files to app.locals space
app.locals = require('./helpers/views-helper');
//Set up Session
app.use(getExpressSessionStore());
//Allow CORS
app.use(cors());
//Set up Database
mongoose.connect(process.env.DB_CONNECTION_STRING)
.then(() => console.log('Connected to MongoDB...'))
.catch(err => console.error('Could not connect to MongoDB...', err));
//Set up Passport
app.use(passport.initialize({}));
app.use(passport.session({}));
app.use(flash());
//Set up a middleware to pass the session user object to response's locals space
//To be accessed in view layer
const { storeSessionToLocals } = require('./middlewares/authMiddlewares');
app.use(storeSessionToLocals);
//Set Routers
const homeRouter = require('./routes/home');
const userRouter = require('./routes/user');
const chatRouter = require('./routes/chat');
const chatMessagesRouter = require('./routes/chat-messages');
app.use('/', homeRouter);
app.use('/', userRouter);
app.use('/chats', chatRouter);
app.use('/chat-messages', chatMessagesRouter);
//Set up Socket.io
const { createIO } = require('./config/socket-io/socketio-config');
const req = require('express/lib/request');
const httpServer = createServer(app);
createIO(httpServer);
//Set up Logging using Morgan
if(process.env.LOG_FORMAT_MORGAN)
app.use(morgan(process.env.LOG_FORMAT_MORGAN));
module.exports = {
app: httpServer
};