forked from wdi-sg/project-4
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
107 lines (96 loc) · 3.1 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
require('dotenv').config()
const mongoose = require('mongoose')
const bodyParser = require('body-parser')
const path = require('path')
const passport = require('./config/ppConfig')
const session = require('express-session')
const flash = require('connect-flash')
const isLoggedIn = require('./middleware/isLoggedIn')
const userAuth = require('./controllers/auth')
const ejsLayouts = require('express-ejs-layouts')
const methodOverride = require('method-override')
const express = require('express')
const morgan = require('morgan')
const chatController = require('./controllers/chatController')
const Message = require('./models/message')
const unirest = require('unirest')
const app = express()
mongoose.Promise = global.Promise
mongoose.connect(process.env.MONGODB_URI || 'mongodb://localhost/chatertain')
app.set('view engine', 'ejs')
app.use(morgan('dev'))
app.use(bodyParser.json())
app.use(bodyParser.urlencoded({ extended: true }))
app.use(ejsLayouts)
app.use(session({
secret: process.env.SESSION_SECRET,
resave: false,
saveUninitialized: true
}))
app.use(flash())
app.use(express.static(path.join(__dirname, 'public')))
app.use(passport.initialize())
app.use(passport.session())
app.use(methodOverride('_method'))
app.use(function (req, res, next) {
res.locals.alerts = req.flash()
res.locals.currentUser = req.user
next()
})
app.get('/', function (req, res) {
res.render('auth/login')
})
app.use('/auth', userAuth)
app.get('/index', isLoggedIn, function (req, res) {
res.render('index')
})
app.use(isLoggedIn)
app.use('/chats', chatController)
const server = app.listen(process.env.PORT || 3000)
console.log('Server UP')
// io listens to server
const io = require('socket.io').listen(server)
global.io = io
console.log('websocket up')
// Set socket.io listeners.
io.on('connection', function (socket) {
console.log('a user connected')
socket.on('disconnect', function () {
console.log('user disconnected')
})
// on chat to start messages
socket.on('chat', function (msg) {
socket.emit('chat', msg)
})
socket.on('messages', function (data) {
// socket.emit('broad', data)
// get API translation
// console.log('am i consuming API?')
unirest.get('https://yoda.p.mashape.com/yoda?sentence=' + data.composedMessage)
.header('X-Mashape-Key', '5ZGQXOI7M0mshOp7RqMZoqeoWvrwp15JVFLjsnBzw4v4s1bi6p')
.header('Accept', 'text/plain')
.end(function (result) {
console.log('am i consuming API?')
console.log('am i posting new msg into database?', data)
console.log('what is the chatroomid?', data.chatroomId)
const reply = new Message({
chatroomId: data.chatroomId,
body: data.composedMessage,
author: data.author,
authorName: data.authorName,
translate: result.body
})
reply.save(function (err, messages) {
if (err) {
console.log(err)
return
}
// to broadcast the message
console.log('is this broadcasting?')
io.sockets.emit('broad', messages)
// socket.emit('broad', messages)
// socket.broadcast.emit('broad', messages)
})
})
})
})