VR/DEBUG-SERVER/server.js

45 lines
1.3 KiB
JavaScript

const http = require("http");
const url = require("url");
const logs = [];
const server = http.createServer((req, res) => {
const parsedUrl = url.parse(req.url, true);
if (req.method === "GET") {
if (parsedUrl.pathname === "/logs") {
// Respond with all logs
res.writeHead(200, { "Content-Type": "application/json" });
res.end(JSON.stringify(logs));
} else if (parsedUrl.pathname === "/clear") {
// Clear all logs
logs.length = 0;
res.writeHead(200, { "Content-Type": "text/plain" });
res.end("Logs cleared\n");
} else {
// Extract the value of the 'message' query parameter
const message = parsedUrl.query.message || "No message provided";
// Log the message
logs.push({ timestamp: new Date().toISOString(), message });
// Log the message to the console
console.log("Received message:", message);
// Send a response to the client
res.writeHead(200, { "Content-Type": "text/plain" });
res.end("Message received\n");
}
} else {
// Handle other HTTP methods (e.g., POST, PUT, DELETE)
res.writeHead(404, { "Content-Type": "text/plain" });
res.end("Not Found\n");
}
});
const PORT = 3000;
server.listen(PORT, () => {
console.log(`Server listening on port ${PORT}`);
});