-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
96 lines (80 loc) · 2.07 KB
/
server.js
File metadata and controls
96 lines (80 loc) · 2.07 KB
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
const port = 12000;
import path from 'path'
import express from 'express'
import sass from 'node-sass-middleware'
import eWs from 'express-ws'
import watch from 'node-watch'
import fs from 'fs'
const __dirname = path.resolve(path.dirname(''))
const app = express()
const expressWs = eWs(app)
app.use(express.json());
app.use(express.urlencoded({
extended: true
}));
app.use(express.static('static'))
app.use(
sass({
src: __dirname + '/sass',
dest: __dirname + '/static'
})
)
app.post('/teste',(req,res)=> {
console.log("query",req.query)
console.log("params",req.params)
console.log("body",req.body)
res.status(200)
res.send('OK')
})
const aWss = expressWs.getWss('/ws-refresh')
app.ws('/ws-refresh', (ws,req) => {
ws.on('message', (data) => {
if(data !== 'ws-refresh: opened connection') {
aWss.clients.forEach(function (client) {
client.close()
});
} else {
aWss.clients.forEach(function (client) {
client.send('connected');
});
}
})
})
const removeAllFiles = (path,callback) => {
const throwError = () => {
callback(false)
console.log(err);
}
fs.readdir(path, (err, files) => {
if (err) return throwError();
for (const file of files) {
fs.unlink(path+"/"+file, err => {
if (err) return throwError();
});
}
callback(true)
})
}
watch('./sass/css', {recursive: true}, (e,name) => {
try {
removeAllFiles('static/css/', (ok) => {
if(ok) aWss.clients.forEach(client => {
client.send('{"event":"refresh","type":"page"}')
})
})
} catch(e) {
console.log(e)
}
})
watch('./static', {recursive: true}, (e,name) => {
try {
aWss.clients.forEach(client => {
client.send('{"event":"refresh","type":"page"}')
})
} catch(e) {
console.log(e)
}
})
app.listen(port,() => {
console.log('listening on ', port)
})