-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.js
66 lines (55 loc) · 1.86 KB
/
server.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')
const app = express()
const path = require('path')
const bodyParser = require('body-parser')
const cookieParser = require('cookie-parser')
const session = require('express-session')({
secret: process.env.SESSION_SECRET || 'aishwarya',
resave: true,
saveUninitialized: true
})
const server = require('http').createServer(app)
const mongoose = require('mongoose')
const chatServer = require('./lib/chatServer')
const users = require('./src/controllers/users.js')
app.use(express.static(path.join(__dirname, 'public/build')))
app.use(express.static(path.join(__dirname, 'public/assets')))
app.use(bodyParser.json())
app.use(bodyParser.urlencoded({extended: true}))
app.use(cookieParser())
app.use(session)
app.use('/io-square', express.static(path.join(__dirname, '/node_modules/io-square-browser/lib')))
// Database connection
mongoose.connect(process.env.MONGODB_URI || 'mongodb://127.0.0.1:27017/geekChat')
const db = mongoose.connection
mongoose.Promise = require('promise')
db.on('error', console.error.bind(console, 'db connection error:'))
db.once('open', (err) => {
if (err) {
throw new Error('Db not connected')
}
server.listen(process.env.PORT || 3000, () => {
console.log('listening on port 3000')
})
console.log('DB connected successfully and APP listening at: ' + Date())
})
app.get('/', (req, res) => {
if (req.session.user_id) {
return res.redirect('/home')
}
return res.sendFile(path.join(__dirname, 'views/index.html'))
})
app.post('/signup', users.addUser)
app.post('/login', users.validateUser)
app.get('/home', (req, res) => {
if (req.session.user_id) {
return res.sendFile(path.join(__dirname, 'public/index.html'))
}
return res.redirect('/')
})
app.get('/userData', users.findUser)
app.get('/logout', (req, res) => {
req.session.destroy()
res.redirect('/')
})
chatServer.listen(server, session)