A Badge On Github

You may have come across fancy looking badges on the README’s of several repos on Github. Some of them indicate status such as whether a npm package package is outdated, if a build has passed or even as simple as a redirect badge.

I am going to take you through a simple way of building a simple badge service. The service is going to check if an API is online or not. We will use Node.js to build the service.

The SVG File

This is the most important file, which will indicate whether my API is online or not. Here is how each looks like:

Online Status Badge

Offline Status Badge

The Code

To get started, initiate the project from your Terminal:

We will use http and fs module for Node.js to build our badge server, hence no external dependencies!

Check out the ngrok version of server.js on the repo

Here is how our server.js file will look like:

Important notes:

The Content-Type should be image/svg+xml Since we are checking if the API is online, we will accept any response, regardless of status code The typical error sent if our API is offline is ECONNREFUSED

To test if the correct status badge is being sent we will create a mock server using the code below:

Embedding The Status Badges

I will be using the brackets markdown preview plugin to see the results.

here is the Markdown snippet:

The results:

It Works!

Further Reading

This is just a simple approach of building a status badge service and consuming it. One can however go ahead and build more complex scenarios such as david-dm with express or even services like gratipay.

I have implemented this project successfully to check if my personal REST API is online or not. It is hosted on my VPS (tunneled through ngrok)and the badge is used on the source code repo.