-
-
Notifications
You must be signed in to change notification settings - Fork 4.1k
/
Copy pathserver.js
62 lines (52 loc) · 1.6 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
const path = require('path')
const fs = require('fs')
const express = require('express')
const app = express()
const { createServer } = require('http')
const { Server } = require('socket.io')
const port = process.env.PORT || 8098
const localIp = require('ip').address()
const httpServer = createServer(app)
const io = new Server(httpServer, {
cors: {
origin: true,
},
})
app.use(express.static(path.join(__dirname, './')))
app.get('/local_ip', function (req, res) {
res.header('Content-Type', 'application/javascript')
res.send(`window.process = {
env: {
PORT: ${port},
HOST: '${localIp}'
}
}`)
})
app.get('/', function (req, res) {
const hookContent = fs.readFileSync(path.join(__dirname, '/build/hook.js'), 'utf8')
const backendContent = fs.readFileSync(path.join(__dirname, '/build/backend.js'), 'utf8')
res.send([hookContent, backendContent].join('\n'))
})
// Middleman
io.on('connection', function (socket) {
// Disconnect any previously connected apps
socket.broadcast.emit('vue-devtools-disconnect-backend')
socket.on('vue-devtools-init', () => {
socket.broadcast.emit('vue-devtools-init')
})
socket.on('disconnect', (reason) => {
if (reason.indexOf('client')) {
socket.broadcast.emit('vue-devtools-disconnect-devtools')
}
})
socket.on('vue-message', data => {
socket.broadcast.emit('vue-message', data)
})
})
httpServer.listen(port, '0.0.0.0', () => {
// eslint-disable-next-line no-console
console.log(`
vue-devtools listening on 0.0.0.0:${port}
open this link http://${localIp}:${port}/app.html when you need remote vue-devtools.
`)
})