This example Node.js application uses Express.js and sets up Airbrake to report errors and performance data. To adapt this example to your app, follow these steps:
npm install @airbrake/node
Include the required Airbrake libraries in your app.js
const Airbrake = require('@airbrake/node');
const airbrakeExpress = require('@airbrake/node/dist/instrumentation/express');
const airbrake = new Airbrake.Notifier({
projectId: process.env.AIRBRAKE_PROJECT_ID,
projectKey: process.env.AIRBRAKE_PROJECT_KEY,
});
This middleware should be added before any routes are defined.
app.use(airbrakeExpress.makeMiddleware(airbrake));
The error handler middleware should be defined last. For more info on how this works, see the official Express error handling doc.
app.use(airbrakeExpress.makeErrorHandler(airbrake));
The last step is to run your app. To test that you've configured Airbrake correctly, you can throw an error inside any of your routes:
app.get('/hello/:name', function hello(_req, _res) {
throw new Error('Hello from Airbrake!');
});
Any unhandled errors that are thrown will now be reported to Airbrake. See the basic usage to learn how to manually send errors to Airbrake.
Note: to see this all in action, take a look at our
example app.js
file
and to run the example, follow the next steps.
If you want to run this example application locally, follow these steps:
git clone [email protected]:airbrake/airbrake-js.git
cd airbrake-js/packages/node/examples/express
npm install
AIRBRAKE_PROJECT_ID=your-id AIRBRAKE_PROJECT_KEY=your-key node app.js
firefox localhost:3000