-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
73 lines (62 loc) · 1.82 KB
/
index.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
71
72
73
// Module imports
import 'babel-polyfill'
import express from 'express'
import nunjucks from 'nunjucks'
import mongoose from 'mongoose'
import session from 'express-session'
import bodyParser from 'body-parser'
const MongoStore = require('connect-mongo')(session)
const app = express()
// Custom imports and variables
import routes from './routes'
import Chat from './models/chat'
// Connect to our database and set up our sessions
mongoose.connect(process.env.MONGODB || `mongodb://localhost/${process.env.npm_package_name}`, {
useNewUrlParser: true
})
const db = mongoose.connection
db.on('error', console.error.bind(console, 'connection error:'))
app.use(session({
secret: 'fluffybunnies1231247753',
resave: false,
saveUninitialized: false,
cookie: {
maxAge: 1800000 // 30 minutes (milliseconds)
},
store: new MongoStore({
mongooseConnection: db
})
}))
// Configure nunjucks
nunjucks.configure('views', {
autoescape: true,
express: app
})
// Configure express
app.set('view engine', 'njk')
app.use(bodyParser.urlencoded({ extended: false }))
app.use(bodyParser.json())
app.use(express.static('./assets'))
app.use(routes.router)
// Start our app
let server = app.listen(process.env.PORT || 3000, () => {
console.log("App is started on port 3000!")
})
// Socket.io for chat
const io = require('socket.io')(server)
io.on('connection', (socket) => {
// not used currently
socket.on('disconnect', function(){
})
// called when client side javascript emits message event
socket.on('message', function(msg) {
Chat.create({
message: msg,
screenName: routes.screenName
}).then((chat) => {
io.emit('message', chat)
})
})
})
// Export our app so we can use for tests
module.exports = app