Express: @trailscope/express
Request logging, error capture and context for Express 4 and 5. Built on @trailscope/node; shared behaviour is in sdk-spec.md.
npm install @trailscope/expressimport express from 'express';import { init, trailscope, trailscopeErrorHandler, setCorrelation, setUser } from '@trailscope/express';
init({ service: 'orders-api' }); // TRAILSCOPE_INGEST_KEY from the environment
const app = express();app.use(trailscope()); // first, so it times everythingapp.use(express.json());app.get('/orders/:id', (req, res) => { setCorrelation('order_id', req.params.id); res.json(loadOrder(req.params.id));});app.use(trailscopeErrorHandler()); // after the routes, before your own error handlerstrailscope(clientOrOptions?)is the middleware. Pass aTrailscopeClient, or options to create the process-wide client. Without arguments it uses the client frominit(), or creates one from theTRAILSCOPE_*variables.trailscopeErrorHandler()records the error (type, message and stack, kept when the response is a 5xx) and passes it on withnext(err), so your own error handlers still answer.- Routes are recorded as Express defines them:
req.baseUrl + req.route.path, so a router mounted at/orderswith/:idgives/orders/:id. - The request body is the parsed
req.body, so register body parsers as usual; the response body is copied as it is written. - The client IP is
req.ip, which follows Express’strust proxysetting. - Call
setUser()in your authentication middleware, andsetCorrelation()wherever a journey key (an order, an account, a session) is known.
Everything from @trailscope/node used in request handlers is re-exported: init, getClient, TrailscopeClient, setUser, setCorrelation, setAttribute, recordError, currentContext, contextOf.
A runnable app is in examples/express.