Integrating with Express
The TrackJS-Node agent provides handlers that can be injected into the Express pipeline. The request middleware should be added first to setup the context and the error middleware should be added last to catch any errors.
var express = require("express");
var TrackJS = require("trackjs-node").TrackJS;
// Install TrackJS as soon as possible
TrackJS.install({
token: "YOUR_TOKEN"
});
var app = express()
// the first handler should be the TrackJS request handler.
.use(TrackJS.Handlers.expressRequestHandler())
// ... your application logic
// the last handler should be the TrackJS error handler.
.use(TrackJS.Handlers.expressErrorHandler({ next: false }))
.listen(PORT_NUMBER);
Request Isolation
Agent metadata and telemetry is isolated between requests. For example, a console.log
message written during a request to /foo
won’t appear on errors captured from /bar
. This enables you to add request-specific metadata to requests.
app.get("/foo", (req, res, next) => { TrackJS.addMetadata({ userStep: "1" }); //... next(); }); app.get("/bar", (req, res, next) => { TrackJS.addMetadata({ userStep: "2" }); //... next(); });