Skip to content

Commit

Permalink
Merge pull request #5 from sqrDAO/fix/show-log-scrape
Browse files Browse the repository at this point in the history
add logs
  • Loading branch information
duonghb53 authored Dec 24, 2024
2 parents 3f9277b + 9ff0260 commit 71f2563
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,34 @@ app.use(bodyParser.json());
// Enable CORS for all routes
app.use(cors());

let logs = [];

// Middleware to capture console logs
const originalLog = console.log;
console.log = (...args) => {
originalLog(...args);
logs.push(args.join(" "));
};

app.get("/logs", (req, res) => {
res.setHeader("Content-Type", "text/event-stream");
res.setHeader("Cache-Control", "no-cache");

// Override console.log to send logs via SSE
const originalLog = console.log;
console.log = (...args) => {
originalLog(...args);
res.write(`data: ${args.join(" ")}\n\n`);
};

// Close connection on client disconnect
req.on("close", () => {
console.log("Client disconnected");
console.log = originalLog; // Restore console.log
res.end();
});
});

// Set up a basic route
app.get("/", (req, res) => {
res.send("Hello, World! This is your Express server.");
Expand Down

0 comments on commit 71f2563

Please sign in to comment.