Star us on GitHub
Star
Menu

Express.js Quick Start

Learn how to set up highlight.io in Express.js.

1

Configure client-side Highlight. (optional)

If you're using Highlight on the frontend for your application, make sure you've initialized it correctly and followed the fullstack mapping guide.

2

Install the relevant Highlight SDK(s).

Install @highlight-run/node with your package manager.

npm install --save @highlight-run/node
Copy
3

Initialize the Highlight JS SDK.

Initialize the Highlight JS SDK with your project ID.

import { H } from '@highlight-run/node' H.init({ projectID: '<YOUR_PROJECT_ID>', serviceName: '<YOUR_SERVICE_NAME>', environment: 'production', })
Copy
4

Add the Express.js Highlight integration.

Use the Node Highlight SDK in your response handler.

import { H, Handlers } from '@highlight-run/node' // or like this with commonjs // const { H, Highlight } = require('@highlight-run/node') const highlightConfig = { projectID: '<YOUR_PROJECT_ID>', serviceName: 'my-express-app', serviceVersion: 'git-sha', environment: 'production' } H.init(highlightConfig) // import express after initializing highlight to automatically instrument express import express from 'express' const app = express() // This should be before any controllers (route definitions) app.use(Handlers.middleware(highlightConfig)) app.get('/', (req, res) => { res.send(`Hello World! 0.9931820449207245`) }) // This should be before any other error middleware and after all controllers (route definitions) app.use(Handlers.errorHandler(highlightConfig)) app.listen(8080, () => { console.log(`Example app listening on port 8080`) })
Copy
5

Try/catch an error manually (without middleware).

If you are using express.js async handlers, you will need your own try/catch block that directly calls the highlight SDK to report an error. This is because express.js async handlers do not invoke error middleware.

app.get('/sync', (req: Request, res: Response) => { // do something dangerous... throw new Error('oh no! this is a synchronous error'); }); app.get('/async', async (req: Request, res: Response) => { try { // do something dangerous... throw new Error('oh no!'); } catch (error) { const { secureSessionId, requestId } = H.parseHeaders(req.headers); H.consumeError( error as Error, secureSessionId, requestId ); } finally { res.status(200).json({hello: 'world'}); } });
Copy
6

Verify that your SDK is reporting errors.

You'll want to throw an exception in one of your express.js handlers. Access the API handler and make sure the error shows up in Highlight.

app.get('/', (req, res) => { throw new Error('sample error!') res.send(`Hello World! 0.41148303711784484`) })
Copy
7

Verify your backend logs are being recorded.

Visit the highlight logs portal and check that backend logs are coming in.

8

Verify your backend traces are being recorded.

Visit the highlight traces portal and check that backend traces are coming in.