Express
This page documents the Bugsee JavaScript SDK, which is in beta (0.1.x) and can change between betas.
@bugsee/express adds a request context, an http.server transaction and error reporting to an Express app on Node.js. Read the backend overview first for how context and trace correlation work.
You need an app token. Create a JavaScript application in Bugsee and choose Express as its target; see Create your app.
Install
- npm
- pnpm
- yarn
- bun
npm install @bugsee/express
pnpm add @bugsee/express
yarn add @bugsee/express
bun add @bugsee/express
The adapter declares no peer dependency. It only relies on the shape of your Express app (Express 4 or 5).
Set up
Call launch() at the top of your server entry, then pass the app to setupExpress:
import { launch, setupExpress } from '@bugsee/express';
import express from 'express';
launch('<your_app_token>');
const app = express();
setupExpress(app);
// ... your routes ...
app.listen(3000);
setupExpress installs the request middleware immediately, so call it before you register routes. It installs the error handler for you:
- It wraps
app.listen, so the handler is added when the server starts listening, after your routes exist. - An app that never calls
app.listen(for example one passed tohttp.createServer) gets the handler on its first request. - The handler is placed in front of any error middleware you already registered, so an error-response middleware that ends the response without calling
next(err)does not hide the error from Bugsee.
Place the error handler yourself
To control the position, turn off the automatic handler and add it with setupExpressErrorHandler:
import { setupExpress, setupExpressErrorHandler } from '@bugsee/express';
setupExpress(app, { autoErrorHandler: false });
// ... your routes ...
setupExpressErrorHandler(app); // reports, then calls next(err)
app.use((err, req, res, next) => {
// your error response
});
For full control, the two middlewares are also exported on their own: requestHandler() (mount it first) and errorHandler() (mount it after your routes).
What it does
- Opens a request context for each request, so everything reported inside a route is tagged with that request.
- Continues an inbound
traceparentheader and records anhttp.servertransaction named after the route pattern, such as/users/:id. - Reports an error that reaches the error handler, with the request context merged in, and then passes it on with
next(err). Your own error handling still runs. - Does nothing, and passes every request through, when no client is launched.
By default an error with no status, or with a status of 500 or higher, is reported, and a 4xx is skipped.
Options
Pass options as the second argument of setupExpress. setupExpressErrorHandler, requestHandler and errorHandler take the same options, without autoErrorHandler.
| Option | Type | Default | Description |
|---|---|---|---|
user | (req) => string | undefined | off | The end-user identity for reports raised during the request. |
shouldReport | (error) => boolean | 5xx or no status | Decides whether a thrown error is reported. |
onError | (error) => void | none | Receives a failure of the adapter itself. It never reaches the request. |
autoErrorHandler | boolean | true | Set false to place the error handler yourself. setupExpress only. |
req is a structural view of the Express request: method, url, originalUrl, route, baseUrl and headers. Cast it to read fields added by your own middleware:
setupExpress(app, {
user: (req) => (req as { user?: { id: string } }).user?.id,
});
Launch options
The adapter does not change the launch options. These are the ones that matter most on a server:
| Option | Default | Description |
|---|---|---|
instrumentIncomingRequests | true | Automatic per-request context and http.server transaction. See the backend overview. |
captureLocalVariables | false | Attach local variables to the stack frames of a reported error. See Diagnostics. |
captureSourceContext | false | Attach the source lines around each stack frame. See Diagnostics. |
detectHangs | true | Report event-loop hangs. See Hangs. |
profiling | false | Attach a rolling CPU profile to reports. See Diagnostics. |
@bugsee/express installs @bugsee/bugsee for you and re-exports everything from its Node entry, so launch and the SDK types come from this one package. You do not install or import @bugsee/bugsee separately. On Bun, see Running on Bun.
Related pages
- Backend overview — request context, trace correlation and incoming-request instrumentation
- Issue detection — what else is detected on a server
- Performance — transactions and spans