Star us on GitHub
Star
Menu

Express.js

Learn how to set up highlight.io in Express.js.
1
Add `tracingOrigins` to your client Highlight snippet.

This backend SDK requires one of the Highlight frontend SDKs to be installed, so please make sure you've followed the fullstack mapping guide first.

H.init("<YOUR_PROJECT_ID>", { tracingOrigins: ['localhost', 'example.myapp.com/backend'], networkRecording: { enabled: true, recordHeadersAndBody: true, }, });
Copy
2
Install the Highlight JS SDK.

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

# with yarn yarn add @highlight-run/node # with pnpm pnpm add @highlight-run/node # with npm npm install @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'})
Copy
4
Add the Express.js Highlight integration.

Use the Node Highlight SDK in your response handler.

import { Handlers } from '@highlight-run/node' // or like this with commonjs // const Highlight = require('@highlight-run/node') const app = express() app.get('/', (req, res) => { res.send(`Hello World! 0.8481760675304113`) }) // This should be before any other error middleware and after all controllers (route definitions) app.use(Handlers.errorHandler({ projectID: 'YOUR_PROJECT_ID' })) 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('/async', async (req: Request, res: Response) => { try { // do something dangerous... throw new Error('oh no!'); } catch (error) { const parsedHeaders = H.parseHeaders(req.headers); H.consumeError( error as Error, parsedHeaders?.secureSessionId, parsedHeaders?.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.397252983216432`) })
Copy
7
Set up logging.

With the JS SDKs, your logs are reported automatically from console methods. See the JS logging setup guide for more details.