Introducing Airbrake With Node Js

Introducing Airbrake With Node Js Explained

If you’re looking for a way to improve your Node.js development workflow, consider using Airbrake. In this article, we’ll give you a brief overview of what Airbrake is and how it can help you track and fix errors in your Node.js applications.

Airbrake Overview

– The official Airbrake notifier for detecting JavaScript errors in Node.js and reporting them to Airbrake.
– Airbrake is available for every language and framework.
– Installed in a minute and takes less time and tracks problems and more time progressed

Why Use Airbrake

1. Developer-centric

Airbrake onboarding is “very easy” for large languages and frameworks. Integration makes Airbrake error monitoring easier, more common, and more useful for developers.

2. Lightweight

A non-standard and non-server design means low maintenance and almost zero technical debt to get the full visibility of every app stack.

3. Immediate Impact

Real-time notifications will get the current effect.
Once Airbrake is installed, you will know more about the current state of your app. Real-time notifications will notify you of new issues and critical errors.

hire node js developer

Features of Airbrake

  • Simple and fast installation options including npm and yarn.
  • Post undetected errors to Airbrake or manually use try/catch.
  • Add parameters to your errors for more context.
  • Control which errors you send with custom filtering options.
  • Easily monitor errors and performance.
  • A new error in your code will be notified to you by email or messaging.
  • Detect the exact event which caused an error so you can understand the issue and resolve it.
  • Airbrake uses protocols that protect your code and sensitive data remains untouchable.

Getting started with Airbrake

First, create an account on Airbrake (https://airbrake.io). They will provide you with keys for each application that you want to keep track of.
– Airbrake dashboard looks like this:

Airbrake dashboard

Create a simple Express app using Node.js
Install the Airbrake package using the following commands
– Using npm: npm install @airbrake/node
– Using yarn: yarn add @airbrake/node
Now, In the app.js file add the following code after the app variable has been declared
1. Air-brake integration

// Air-brake integration
		const airbrake = new Airbrake.Notifier({
		projectId: '<YOUR_AIRBRAKE_PROJECT_ID>',
		projectKey: '<YOUR_AIRBRAKE_API_KEY>',
		});

2. Add middleware before the routes defined

// This middleware should be added before any routes are defined
		app.use(airbrakeExpress.makeMiddleware(airbrake));

3. Add error handler middleware for Airbrake

// The error handler middleware for Airbrake
		app.use(airbrakeExpress.makeErrorHandler(airbrake));

4. Final app.js looks like this:

var createError = require('http-errors');
		var express = require('express');
		var path = require('path');
		var cookieParser = require('cookie-parser');
		var logger = require('morgan');
		const Airbrake = require('@airbrake/node');
		const airbrakeExpress = require('@airbrake/node/dist/instrumentation/express');
		
		var indexRouter = require('./routes/index');
		var scheduleRouter = require('./routes/scheduler');
		
		var app = express();
		
		// view engine setup
		app.set('views', path.join(__dirname, 'views'));
		app.set('view engine', 'pug');
		
		app.use(logger('dev'));
		app.use(express.json());
		app.use(express.urlencoded({ extended: false }));
		app.use(cookieParser());
		app.use(express.static(path.join(__dirname, 'public')));
		
		
		// Air-brake integration
		const airbrake = new Airbrake.Notifier({
		projectId: '<YOUR_AIRBRAKE_PROJECT_ID>',
		projectKey: '<YOUR_AIRBRAKE_API_KEY>',
		});
		
		// This middleware should be added before any routes are defined
		app.use(airbrakeExpress.makeMiddleware(airbrake));
		
		app.use('/', indexRouter);
		app.use('/scheduler', scheduleRouter);
		
		
		// The error handler middleware for Airbrake
		app.use(airbrakeExpress.makeErrorHandler(airbrake));
		module.exports = app;

Setup/Add created project to Airbrake

– After adding this project to Airbrake you can see the project here

adding this project to Airbrake

– You can get the project Id and project key by clicking on the project setting. Paste the keys to your code.

project setting

Now if any error occurs in the code it will be shown on Airbrake.

Error Code Shown