-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexpress.js
50 lines (40 loc) · 1.14 KB
/
express.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
const express = require('express');
const fs = require('fs');
const path = require('path');
const WebSocket = require('ws');
const app = express();
// const wss = new WebSocket.Server({ server });
const ws = new WebSocket('ws://localhost:8796');
const toJSON = (data) => {
return JSON.stringify(data);
}
app.get('/', (req, res) => {
const targetNodePath = path.join(__dirname, './test.js');
const { spawn } = require('child_process');
const process = spawn('node', [targetNodePath]);
process.stdout.on('data', (data) => {
console.log('Received chunk data', data.toString());
// Send log data to the existing WebSocket server
if (ws.readyState === WebSocket.OPEN) {
ws.send(toJSON({
type: 'log',
msg: data.toString()
}));
}
});
process.on('close', (code) => {
console.log(`Child process exited with code ${code}`);
// Notify the WebSocket server that the process has exited
if (ws.readyState === WebSocket.OPEN) {
ws.send(toJSON({
type: 'exit',
msg: code
}));
}
});
res.json({ msg: 'Process started' });
})
const PORT = 3003;
app.listen(PORT, () => {
console.log(`Server running on port ${PORT}`);
});