Trasys
Node.js SDKFrameworks

Express

Mount the Trasys SDK's Express middleware as the first app.use() call to capture every inbound request as a span before your other application middleware runs.

Express

// index.js
require('dotenv').config();
const { sdk, logger } = require('./trasys'); // SDK must be required first

const express = require('express');
const app = express();

// Mount Trasys before everything else
app.use(sdk.middleware());

// Then your other middleware
app.use(express.json());

app.get('/users/:id', async (req, res) => {
  const user = await db.findUser(req.params.id);
  res.json(user);
});

app.listen(3000);

Mount sdk.middleware() before all other middleware. If mounted after body parsers or route handlers, some request attributes (duration, route name) may not be captured correctly.

TypeScript

// index.ts
import 'dotenv/config';
import { sdk, logger } from './trasys';

import express from 'express';

const app = express();

app.use(sdk.middleware());
app.use(express.json());

app.get('/users/:id', async (req, res) => {
  const user = await db.findUser(req.params.id);
  res.json(user);
});

app.listen(3000);

Route parameter normalization

The middleware captures the parameterized route pattern (/users/:id) rather than the resolved URL (/users/123). This works because Express sets req.route.path after the router matches — the middleware reads it at response finish time, not at request start.

No configuration is required for this behavior.

Error handler integration

Place the Trasys middleware before your error handler. Errors thrown in route handlers are captured automatically as part of the span.

app.use(sdk.middleware());

app.get('/orders/:id', async (req, res) => {
  const order = await db.findOrder(req.params.id);
  if (!order) throw new Error('Order not found'); // captured automatically
  res.json(order);
});

// Custom error handler (optional)
app.use((err, req, res, next) => {
  logger.error('Unhandled error', { error: err, path: req.path });
  res.status(500).json({ error: 'Internal server error' });
});

Next steps

On this page