Express
Capture Express requests, middleware, handlers, responses, and child operations.
Installation
npm install server-devtools express
npm install -D tsx @types/expressTypeScript setup
Create the preloaded initialization module:
import ServerDevTools from "server-devtools";
export const devtools = new ServerDevTools({
auth: {
username: process.env.SERVER_DEVTOOLS_USERNAME!,
password: process.env.SERVER_DEVTOOLS_PASSWORD!,
},
});
await devtools.start();Mount the same instance before your routes:
import express from "express";
import { devtools } from "../server-devtools";
const app = express();
app.use(express.json());
app.use((req, res, next) => devtools.middleware(req, res, next));
app.get("/users/:id", (req, res) => {
res.json({ id: req.params.id });
});
app.listen(3000);Run with preload:
npx tsx watch --import ./server-devtools.ts src/main.tsJavaScript setup
The package also exposes a CommonJS entry point:
const ServerDevTools = require("server-devtools");
const devtools = new ServerDevTools({
auth: {
username: process.env.SERVER_DEVTOOLS_USERNAME,
password: process.env.SERVER_DEVTOOLS_PASSWORD,
},
});
module.exports = { devtools };
void devtools.start();Start with node --import ./server-devtools.js src/main.js, then mount devtools.middleware before routes.
Run and verify
Open http://localhost:3000/_devtools, sign in, and call /users/123. Express middleware and handler spans appear beneath the HTTP request. Request bodies are captured by the Express instrumentation when available.
Troubleshooting
- No request: preload the initialization file before importing Express.
- No body: ensure a body parser such as
express.json()runs and the request has a body. - Duplicate traces: create one SDK instance and share it across the app.
Last updated on