-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
33 lines (27 loc) · 883 Bytes
/
server.js
File metadata and controls
33 lines (27 loc) · 883 Bytes
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
const http = require("http")
const fs = require('fs')
// 相当于手动构建一个 static server
const server = http.createServer((request, response) => {
if (request.url === '/') {
response.writeHead(200, {"Content-Type": "text/html"})
const html = fs.readFileSync('./index.html', 'utf-8')
response.write(html)
} else if (request.url === '/index.js') {
response.writeHead(200, {"Content-Type": "application/javascript"})
const js = fs.readFileSync('./index.js', 'utf-8')
response.write(js)
}
response.end()
})
const io = require('socket.io')(server)
io.on('connection', client => {
fs.watchFile('./index.html', (curr, prev) => {
console.log('index.html 文件变动')
io.emit('reload');
})
fs.watchFile('./index.js', (curr, prev) => {
console.log('index.js 文件变动')
io.emit('reload');
})
})
server.listen(8000)