forked from rizaradiarivaldo/HireJob-API
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
117 lines (95 loc) · 3.12 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
108
109
110
111
112
113
114
115
const express = require('express');
const cors = require('cors')
const bodyParser = require('body-parser')
const perekrutRouter = require('./src/routes/perekrut')
const pekerjaRouter = require('./src/routes/pekerja')
const pengalamanRouter = require('./src/routes/pengalaman')
const hireRouter = require('./src/routes/hire')
const hireModel = require('./src/models/hire')
const portofolioRouter = require('./src/routes/portofolio')
const { PORT } = require('./src/helpers/env')
const path = require('path')
const ejs = require('ejs')
const socketio = require('socket.io')
const http =require ('http')
const app = express();
const server = http.createServer(app)
const io = socketio(server)
app.set('views', path.join(__dirname,'src/views'))
app.set('view engine', 'ejs')
app.use(bodyParser.urlencoded({ extended: false}))
app.use(bodyParser.json())
app.use(express.static(path.join(__dirname, './dist')))
app.use('*', (req,res) => {
res.sendFile(__dirname, './dist/index.html')
})
app.get('/*', (req,res) => {
res.sendFile(path.join(__dirname, './dist/index.html'))
})
app.use(express.static('src/uploads'))
app.use(express.static('src/views'))
app.use(cors())
app.use('/api/v1/pekerja', pekerjaRouter)
app.use('/api/v1/perekrut', perekrutRouter)
app.use('/api/v1/pengalaman', pengalamanRouter)
app.use('/api/v1/hire', hireRouter)
app.use('/api/v1/portofolio', portofolioRouter)
io.on('connection', (socket) => {
console.log('user connected')
socket.on('join-room', (payload) => {
socket.join(payload)
})
socket.on('get-all-pekerja', (payload) => {
hireModel.cariPekerja(payload.idperekrut)
.then((result) => {
if(result.length === 0) {
console.log('pekerja not found')
} else {
io.emit('listPekerja', result)
}
})
.catch((err) => {
console.log(err)
})
})
socket.on('get-all-perekrut', (payload) => {
hireModel.cariPerekrut(payload.idpekerja)
.then((result) => {
if(result.length === 0) {
console.log('perekrut not found')
} else {
io.emit('listPerekrut', result)
}
})
.catch((err) => {
console.log(err)
})
})
socket.on('get-history', (payload) => {
hireModel.getMessages(payload)
.then((result) => {
console.log(result)
io.to(payload.sender).emit('historyMessage', result)
}).catch((err)=> {
console.log(new Error(err))
})
})
socket.on('send-message', (payload) => {
hireModel.sendMessage(payload)
.then((result) => {
console.log(`${payload.sender} ${payload.receiver} ${payload.message}`)
const room = payload.receiver
io.to(room).emit('private-message', {
sender: payload.sender,
msg: payload.message,
receiver: room
})
})
.catch((err) => {
console.log(err)
})
})
})
server.listen(PORT, () => {
console.log(`App is running at port ${PORT}`)
});