Building API with Express.js and Hadron – Hacker Noon


Together with folks at Brainhub, we have developed a tool, with some of magic that dependency injection provides, to make it more enjoyable and easy to implement CRUD API.

Hadron is a lightweight, open-source framework that can be used with some tools like Express.js (at the moment, Express is the only supported web framework) and database management tools like TypeORM. Hadron even consists of packages like hadron-serialization, which makes it possible to simply structure the data that API outputs, even with selected fields visible only to some “groups”.

Hadron does not affect backend performance in any negative way, it’s aim is to improve the experience while building and maintaining API and providing high efficiency.

A short overview of Hadron + use cases

Setting up the backend with Hadron requires just a few lines of code. Firstly, I will show you how to set up a basic hello world route using Hadron and Express.js.

A hello-world example built with Hadron

The most important thing here is the config object. We specify there the configuration for Hadron packages (routes for hadron-express). The constructor method returns a promise with the container object, which is used for dependency injection. We will dig deeper into that later in this article.

As you’ve probably noticed, developing a larger app can be a nightmare while using more Hadron packages. The config object would be massive — just imagine all routes there. Of course, we can merely divide routes to multiple files and import them.

This is where the hadron-json-provider package comes in. This standalone package allows you to specify the path and extensions for files to be automatically imported for you.

It is quite useful for splitting our routes into different files. By default, the hadron-express package accepts key routePaths in Hadron’s config which accepts an array of paths and extensions and then uses the hadron-json-provider package to load routes from these files.

Hadron vs plain express.js

Remember that Hadron does not affect the performance of your backend application — tested using api-benchmark with 10000 runs sampled with concurrency: 10000.

Hadron provides you the space and tools that boost productivity and maintainability over your project and creates an abstract layer over frameworks like Express.js.

The main advantage of using Hadron is that you can quickly and easily select various tools to manage databases, provide security, and shape your output based on user type. Everything is simply done for you and easily configured by you.

Creating API using Hadron is so simple that you can split your routes into different files without caring about dependencies, instances, etc. This is because of dependency injection, which is used in the route’s callback function.

When you register any package, it is initialized using the provided config and put in the container under the specific key(s) that you can later use in the route’s callback functions.

Hadron’s dependency injection

Hadron provides the option to store data or object instances in something we call the container. It also keeps data initialized by Hadron packages, for example, database repositories in the hadron-typeorm package.

While registering an item in the container, you can choose one of the lifetime options, which are:

  • value — this is the default behavior. While getting data from the container, you get back the same thing that was registered.
  • singleton — always returns the same instance of registered class/constructor function.
  • transient — always returns a new instance of registered class/constructor function.

Dependency injection is used in the route’s callbacks, which allows you to access any container values easily. Let’s assume we will store a string Hello World in our container under the key message and we would like to access it on our route.

After sending a request to http://localhost:8080/, you will receive:

"Message stored in container: Hello World"

That is how you can access container items. Most packages store items there so you can easily perform some actions or retrieve data you want.

You may now think about accessing request variables that are very often used in our routes like headers, params, body, query etc.. These data are provided as the first parameter in the callback’s function.

In this case, the route’s response will return the requests’ header object.

Hadron packages quick overview

We’ve only discovered a few packages so far, especially hadron-express. Let’s discuss some of the official Hadron packages.

All examples presented in this article are available at GitHub here. Feel free to play with them.

read original article here