File size: 896 Bytes
372c408
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import type { IncomingMessage, ServerResponse } from "node:http";
import type { PreviewServer, ViteDevServer } from "vite";

export function requestLoggingServerHook<
  T extends ViteDevServer | PreviewServer,
>(server: T) {
  server.middlewares.use(
    (
      request: IncomingMessage,
      response: ServerResponse,
      next: () => void,
    ) => {
    const startTime = Date.now();
    const { method, url } = request;
    const timestamp = new Date().toISOString();

    // Log the incoming request
    console.log(`[${timestamp}] ${method} ${url}`);

    // Capture the response finish event to log response details
    response.on("finish", () => {
      const duration = Date.now() - startTime;
      const { statusCode } = response;
      console.log(
        `[${timestamp}] ${method} ${url} - ${statusCode} (${duration}ms)`,
      );
    });

    next();
  });
}

// Made with Bob