Add request logging and error handling to artifact and frontend servers (#4829)

Co-authored-by: Claude <noreply@anthropic.com>
This commit is contained in:
Suchintan 2026-02-21 23:15:35 -05:00 committed by GitHub
parent 96343ab525
commit 43781af7c0
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 60 additions and 2 deletions

View file

@ -6,9 +6,32 @@ const app = express();
app.use(cors());
// Request logging middleware — logs method, path, status, and duration
app.use((req, res, next) => {
const start = Date.now();
res.on("finish", () => {
const duration = Date.now() - start;
const timestamp = new Date().toISOString();
const artifactPath = req.query.path || "";
console.log(
"[%s] %s %s %d %dms %s",
timestamp,
req.method,
req.path,
res.statusCode,
duration,
artifactPath,
);
});
next();
});
app.get("/artifact/recording", (req, res) => {
const range = req.headers.range;
const path = req.query.path;
if (!path || !range) {
return res.status(400).send("Missing path or range header");
}
const videoSize = fs.statSync(path).size;
const chunkSize = 1 * 1e6;
const start = Number(range.replace(/\D/g, ""));
@ -50,4 +73,21 @@ app.get("/artifact/text", (req, res) => {
res.send(contents);
});
app.listen(9090);
// Error handling middleware — catches unhandled errors in routes
app.use((err, req, res, _next) => {
const timestamp = new Date().toISOString();
console.error(
"[%s] ERROR %s %s:",
timestamp,
req.method,
req.path,
err.message,
);
res.status(500).send("Internal server error");
});
app.listen(9090, () => {
console.log(
`[${new Date().toISOString()}] Artifact server running at http://localhost:9090`,
);
});

View file

@ -6,6 +6,22 @@ const port = 8080;
const url = `http://localhost:${port}`;
const server = createServer((request, response) => {
const start = Date.now();
// Hook into response finish to log status
response.on("finish", () => {
const duration = Date.now() - start;
const timestamp = new Date().toISOString();
console.log(
"[%s] %s %s %d %dms",
timestamp,
request.method,
request.url,
response.statusCode,
duration,
);
});
// You pass two more arguments for config and middleware
// More details here: https://github.com/vercel/serve-handler#options
return handler(request, response, {
@ -20,6 +36,8 @@ const server = createServer((request, response) => {
});
server.listen(8080, async () => {
console.log(`Running at ${url}`);
console.log(
`[${new Date().toISOString()}] Frontend server running at ${url}`,
);
await open(url);
});