-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
53 lines (42 loc) · 1.33 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
import {router} from './router.js'
import express from 'express'
import { fileURLToPath } from 'url';
import { dirname, join } from 'path';
import cors from 'cors';
import http from 'http';
import WebSocket, {WebSocketServer} from 'ws'
let app = express();
// const currentFileUrl = import.meta.url;
// const currentFilePath = fileURLToPath(currentFileUrl);
//
// const currentDirectory = dirname(currentFilePath);
const options = {
'credentials': true,
'origin': true,
'methods': 'GET,HEAD,PUT,PATCH,POST,DELETE',
'allowedHeaders': 'Authorization,X-Requested-With,X-HTTPMethod-Override,Content-Type,Cache-Control,Accept',
}
app.use(cors(options))
app.set('view engine', 'pug');
app.set('views', './src/views');
app.use(express.static('src'));
app.use('/', router);
const httpServer = http.createServer(app);
httpServer.listen(3000, () => {
console.log('Server is running on 3000');
});
const server = new WebSocketServer({ port: 8080 });
server.on('connection', ws => {
ws.on('message', message => {
if (message.toString().split(' ')[2] === 'exit') {
ws.close()
} else {
server.clients.forEach(client => {
if (client.readyState === WebSocket.OPEN) {
client.send(message.toString())
}
})
}
})
});
export default app;