Set up real-time error notifications for your Node.js applications in 15 minutes or less
Tags: node, cloud, microservices, serverless
Published: 2020-12-15 15:52:20

## What happens when things go bump in the night?
What happens when your cloud-based Node.js applications run into errors that affect your users? Do you get notified? If you want your users to stay your users and not become your competitor's customers, it's crucial to have a monitoring and notification system in place. Today I'm going to show you how to set up a solution for just that in 15 minutes or less.

Cloud applications and technologies are great and afford plenty of advantages, but they have plenty of downsides too—gaining visibility into them can be difficult. I've spent endless hours hunting down errors in these types of environments, and it's not fun. I frequently found myself having to dig through dozens of logs before I could identify errors successfully—if I even detected them at all. 

That's why a friend and I built [codelighthouse.io](https://codelighthouse.io)—to help developers find and fix errors faster.

Today I'm going to show you how you can use [codelighthouse.io](https://codelighthouse.io) to get real-time application error notifications sent straight to developers.

## Getting Started
CodeLighthouse works by plugging a Node.js SDK into your code that automatically catches uncaught exceptions and unhandled promise rejections. The SDK also provides some other neat functionalities that I'll review more in-depth.

### Installing the CodeLighthouse Node.js SDK
Adding our Node.js SDK to your project with NPM couldn't be easier:
```
npm install codelighthouse
```

### Getting your API Key
To get started with the SDK, you'll need to [sign up for a free account](https://codelighthouse.io/#pricing) at [codelighthouse.io](https://codelighthouse.io). Once you sign up, you'll be redirected to your admin dashboard where you can find the organization name you signed up with and your API key:

 ![admin_organization_dashboard](https://dev-to-uploads.s3.amazonaws.com/i/ypfx5wf9dt0v52v9d8gn.png)

Go ahead and note both of these down. We recommend copying/pasting the API key straight from the admin panel to avoid typing errors, and we provide a handy link right below it to do exactly that.

### Configuring the SDK
Importing and configuring the SDK is super easy:
{% gist https://gist.github.com/K-Mistele/4e04510bd9113dd11222edb404949162 %}

Note that your `organization_name` and `api_key` are the values you copied down earlier. You can find them in your admin panel [here](https://codelighthouse.io/admin/organization).

The `default_email` should be the email address you signed up with. 

### Inviting Users
We designed CodeLighthouse with the complexity of distributed agile teams in mind, so collaboration is a key design feature. You can invite additional users to your CodeLighthouse organization via the [user management page](https://codelighthouse.io/admin/users). Once they accept the invitation, you can choose to send error notifications for the application to them by specifying their email address in `default_email` instead. Users in your organization can [log in](https://codelighthouse.io/login) and configure their notification settings and view errors in the error feed.

Once you've got the SDK imported into your code, you have a couple of options on how to use it:

### The Global Exception Handler
By default, CodeLighthouse will automatically catch all uncaught exceptions and unhandled promise rejections. Application error notifications will be sent to the user specified by the `default_email` configuration option. This can be you, or another user in your CodeLighthouse organization.

The global exception handler can be disabled by passing the keyword argument `enable_global_handler=false` to the SDK configuration.

It's important to note that this might not always behave as expected if you're using frameworks like Express.js since they often implicitly provide their own error handlers. For example, Express will catch exceptions inside of routes and handle them before they reach our global exception handler. Fortunately, we have a solution.

### CodeLighthouse's Express.js integration
I personally love Express.js, and I'm not the only one - 73% of Node.js developers use it. Its overwhelming popularity made supporting it an easy call. We've made it easy to report application errors that happen inside your Express app:

{% gist https://gist.github.com/K-Mistele/aba44082c94f10c42f84443be2dd4c98 %}

### Manually catching exceptions
Of course, we anticipate that many developers will already be performing exception handling in their code, but may want to send & receive notifications for those handled exceptions anyways. Our SDK provides an easy way for you to do this as well:

{% gist https://gist.github.com/K-Mistele/c0cddaaa9a668841c9d967401d96439b %}

As the example demonstrates, you can either send notifications to the default user specified in the SDK, or to another user in your CodeLighthouse organization. You can view and invite additional users to your CodeLighthouse organization on the [user management page in your dashboard](https://codelighthouse.io/admin/users).

## Still want to know more?
Do you have any questions? Are you looking for tech support, support for another language, or a plan tailored specifically to your organization's needs? Check out our [documentation page](https://docs.codelighthouse.io), reach out to us at hello@codelighthouse.io, or visit our [contact page](https://codelighthouse.io/contact)!

I look forward to hearing what y'all think in the comments below!