-
Notifications
You must be signed in to change notification settings - Fork 3
/
app.js
70 lines (54 loc) · 1.58 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
68
69
70
//jshint esversion:6
require('dotenv').config();
const express = require('express');
const favicon = require('serve-favicon');
const path = require('path');
const cors = require('cors');
const mongoose = require('mongoose');
const passport = require('passport');
const session = require('express-session');
const LocalStrategy = require('passport-local').Strategy;
const User = require('./models/User');
//Import Routes
const routes = require('./routes');
const app = express();
const port = process.env.PORT || 3000;
const server = app.listen(port, () => {
console.log(`Server Started at port ${port}`);
});
const io = require('socket.io')(server);
//Set socket.io
app.set('socketio', io);
//Set view engine
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');
//Cross origin policy
app.use(cors());
app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')));
app.use(express.json());
app.use(express.urlencoded({
extended: true
}));
app.use(session({
secret: process.env.SECRET_KEY,
resave: false,
saveUninitialized: false
}));
app.use(passport.initialize());
app.use(passport.session());
//Set static public data
app.use(express.static(path.join(__dirname, 'public')));
//Routes to /routes/index.js
app.use(routes);
//Passport config
passport.use(new LocalStrategy(User.authenticate()));
passport.serializeUser(User.serializeUser());
passport.deserializeUser(User.deserializeUser());
//Connect to DB
mongoose.connect(
process.env.DB_CONNECTION, {
useNewUrlParser: true
},
() => console.log('Connected to DB!')
);
mongoose.set('useCreateIndex', true);