Express Middleware
The Astralog Express middleware intercepts standard Node.js HTTP events to track latencies and statuses. It leverages the background event loop, meaning your Express routes remain blazing fast.
Installation
npm install astralog-express astralog-node
Setup
// app.ts
import express from "express";
import { Client } from "astralog-node";
import { astralogMiddleware } from "astralog-express";
const client = new Client({ apiKey: "ast_sk_live_xxxxxxxxxxxxx" });
const app = express();
// Mount the middleware before your routes
app.use(astralogMiddleware(client));
app.get("/checkout", (req, res) => {
// Manual logging will continue to work perfectly in parallel
client.info("User accessed checkout", { user_id: "usr_123" });
res.json({ status: "success" });
});
How it works
The middleware attaches an event listener to the native res.on('finish') event. When your route sends the response to the user, Astralog calculates the exact latency_ms, captures the HTTP status_code, and automatically generates a distributed X-Request-Id header to track the lifecycle.
Because it uses the native Node.js event architecture, it introduces 0ms of blocking latency to your HTTP response times.