-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathapp.js
66 lines (52 loc) · 1.76 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
const express = require('express'),
http = require('http');
const socketio = require('socket.io');
const cors = require('cors');
const path = require('path');
const socketHandler = require('./web-socket/socketHandler');
const mongoose = require('mongoose');
const session = require('express-session');
const passport = require('passport');
const MongoStore = require('connect-mongo')(session);
const PORT = process.env.PORT || 4000;
const app = express();
const server = http.createServer(app);
const io = socketio(server, {
pingTimeout: 30000
});
app.use(cors({ credentials: true, origin: `http://localhost:3000` }));
// app.use(cors({ credentials: true, origin: `${process.env.HEROKU_URL}` }));
//Bodyparser
app.use(express.urlencoded({ extended: true }));
app.use(express.json({limit: '10mb'}));
// Passport config
require('./config/passport')(passport);
// DB Config
const db = require('./config/keys').MongoURI;
//Connect to Mongo
mongoose
.connect(db, { useNewUrlParser: true, useUnifiedTopology: true })
.then(() => console.log('MongoDB Connected...'))
.catch(e => console.log(e));
//Express Session
app.use(
session({
secret: `${process.env.SECRET}`,
store: new MongoStore({ mongooseConnection: mongoose.connection }),
resave: false,
saveUninitialized: false
})
);
//Passport middleware
app.use(passport.initialize());
app.use(passport.session());
io.on('connection', socket => socketHandler(io, socket));
//Routes
app
.use('/messages-route', require('./routes/messages'))
.use('/users', require('./routes/users'));
app.use(express.static(path.join(__dirname, 'client/build')));
app.get('*', (req, res) => {
res.sendFile(path.join(__dirname + '/client/build/index.html'));
});
server.listen(PORT, console.log(`Server started on port ${PORT}`));