-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
69 lines (57 loc) · 1.84 KB
/
server.js
File metadata and controls
69 lines (57 loc) · 1.84 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
const express = require('express');
const path = require('path');
const app = express();
const port = 3000
const { spawn } = require('child_process'); //spawn function from child_process module
const fs = require('fs');
const readline = require('readline');
const filePath = 'deleted_messages.txt';
let lastReadPosition = 0;
app.use(express.urlencoded({ extended: true}))
app.use(express.static('frontend'));
app.get("/",(req,res)=>{
res.sendFile(path.join(__dirname, "/frontend/connect.html"));
})
app.post("/delete",(req,res)=>{
const { token, channel} = req.body;
console.log(token,channel)
const python_process = spawn('python',['backend/DiscordApp.py',token, channel]);
python_process.stdout.on('data', (data)=>{
const message = data.toString();
console.log(message);
});
python_process.on('error', (error) => {
console.error(`Error: ${error.message}`);
});
python_process.on('close', (code) => {
console.log(`Child process exited with code ${code}`);
});
});
app.get("/messages", (req,res) => {
//read .txt file and create array of lines
//send array of lines
const fileStream = fs.createReadStream(filePath,{ start: lastReadPosition, encoding: 'utf8' } )
const rl = readline.createInterface({
input: fileStream,
crlfDelay:Infinity,
});
const lines = []
rl.on('line', (line) => {
lines.push(line);
});
rl.on('close',()=>{
lastReadPosition = fs.statSync(filePath).size;
res.send(lines)
});
// fs.readFile('deleted_messages.txt', 'utf8', (err,data)=>{
// res.send(data);
// })
});
app.listen(port, (error)=>{
console.log("Listening on port: "+ port)
if(error){
console.log("something went wrong");
}else{
console.log("Server is listening on port " + port)
}
})