-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
121 lines (105 loc) · 3.37 KB
/
Copy pathmain.js
File metadata and controls
121 lines (105 loc) · 3.37 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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
const http = require("http");
const url = require("url");
const jwt = require("jsonwebtoken");
const spawn = require("child_process").spawn;
const path = require("path");
let serviceProcess = null;
const server = http.createServer((req, res) => {
const parsedUrl = url.parse(req.url, true);
const startService = async () => {
let done = false;
const processFilePath = path.resolve(__dirname, "index.js");
serviceProcess = spawn("node", [processFilePath]);
serviceProcess.stdout.on("data", function (data) {
const dataString = data.toString();
if (!done && dataString.indexOf("-----") === -1) {
done = true;
return;
}
console.log(dataString);
});
};
const serviceEnd = async (req, res) => {
serviceProcess.kill();
};
// Set CORS headers
res.setHeader("Access-Control-Allow-Origin", "http://localhost:3000");
res.setHeader("Access-Control-Allow-Methods", "POST");
res.setHeader("Access-Control-Allow-Headers", "Content-Type");
// Check if the request is for the '/login' endpoint
if (parsedUrl.pathname === "/login" && req.method === "POST") {
let body = "";
req.on("data", (chunk) => {
body += chunk.toString();
});
// Parse the request body once it's complete
req.on("end", () => {
try {
const data = JSON.parse(body);
if (
data.email == "personal.codemaker@gmail.com" &&
data.password == "aaaaaa"
) {
const secretKey = "abcdefg12345ABCDZZXC!@#";
const payload = {
email: "personal.codemaker@gmail.com",
password: "aaaaaa",
};
const options = {
expiresIn: "1h",
};
const token = jwt.sign(payload, secretKey, options);
console.log("token", token);
res.writeHead(200, { "Content-Type": "application/json" });
res.end(
JSON.stringify({ message: "Success", token: `Bearer ${token}` })
);
} else {
res.writeHead(200, { "Content-Type": "application/json" });
res.end(JSON.stringify({ message: "Faild" }));
}
} catch (error) {
console.error("Error parsing JSON:", error);
res.writeHead(400, { "Content-Type": "application/json" });
res.end(JSON.stringify({ error: "Invalid JSON" }));
}
});
} else if (parsedUrl.pathname === "/mainPage" && req.method === "POST") {
let body = "";
req.on("data", (chunk) => {
body += chunk.toString();
});
// Parse the request body once it's complete
req.on("end", async () => {
try {
const data = JSON.parse(body);
console.log("Received parameters:", data);
if (data.status == "Start") {
console.log("Started");
await startService();
res.writeHead(200, { "Content-Type": "application/json" });
res.end(JSON.stringify({ message: "Success" }));
} else if (data.status == "Stop") {
console.log("Stoped");
await serviceEnd();
res.writeHead(200, { "Content-Type": "application/json" });
res.end(JSON.stringify({ message: "Success" }));
} else {
console.log("Faild");
res.end(JSON.stringify({ message: "Faild" }));
}
} catch (error) {
console.error("Error parsing JSON:", error);
res.writeHead(400, { "Content-Type": "application/json" });
res.end(JSON.stringify({ error: "Invalid JSON" }));
}
});
} else {
res.writeHead(200, { "Content-Type": "text/plain" });
res.end("Not Allowed");
}
});
const port = 3001;
server.listen(port, () => {
console.log(`Server is running on http://localhost:${port}`);
});