Skip to main content

Express

Beta

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 install @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:

server.ts
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 to http.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:

server.ts
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 traceparent header and records an http.server transaction 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.

OptionTypeDefaultDescription
user(req) => string | undefinedoffThe end-user identity for reports raised during the request.
shouldReport(error) => boolean5xx or no statusDecides whether a thrown error is reported.
onError(error) => voidnoneReceives a failure of the adapter itself. It never reaches the request.
autoErrorHandlerbooleantrueSet 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:

OptionDefaultDescription
instrumentIncomingRequeststrueAutomatic per-request context and http.server transaction. See the backend overview.
captureLocalVariablesfalseAttach local variables to the stack frames of a reported error. See Diagnostics.
captureSourceContextfalseAttach the source lines around each stack frame. See Diagnostics.
detectHangstrueReport event-loop hangs. See Hangs.
profilingfalseAttach 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.

Found an issue, typo, or wrong statement on this page? Report it now →