-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
69 lines (63 loc) · 2.05 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
const express = require('express')
const morgan = require('morgan')
const logger = require('./utils/logger')
const userModel = require('./model/user.model')
const app = express()
const path = require('path')
const bodyParser = require('body-parser')
const server = require('http').createServer(app)
const io = require('socket.io')(server)
const routes = require('./routes')
const config = require('config')
const port = process.env.PORT || config.get('app.port')
const host = process.env.DOMAIN || config.get('app.host')
const moment = require('moment')
moment.locale('el')
server.listen(port, () => {
logger.appStarted(port, host)
})
app.use(function (req, res, next) {
res.header('Access-Control-Allow-Origin', '*')
res.header('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept')
next()
})
app.use(express.static(path.join(__dirname, 'public')))
app.set('views', path.join(__dirname, 'views'))
app.set('view engine', 'jade')
app.use(morgan(process.env.NODE_ENV === 'production' ? ('combined', { 'stream': logger.stream }) : 'dev'))
app.use(bodyParser.json())
app.use(bodyParser.urlencoded({ extended: true }))
// Routing
app.get('/', function (req, res) {
res.send('hello world')
})
app.use('/api', routes.user)
app.use('/api', routes.song)
app.use((err, req, res, next) => {
if (err) {
logger.error(err)
return res.status(500).send('Some error occured.')
}
})
io.on('connection', function (socket) {
logger.info('someone connected')
socket.on('userUpdateListenSong', async ({ user, song }) => {
try {
const userData = await userModel.get(user)
userData.currently_playing = {
song_id: song.id || '',
song_artist: song.artist || '',
song_name: song.name || '',
song_genre: song.genre || '',
song_duration: song.duration || ''
}
await userModel.save(userData)
io.sockets.emit('userListensSong', { user: userData })
} catch (err) {
logger.error('err', err)
}
})
socket.on('disconnect', function () {
logger.info('someone disconnected')
})
})