avatarDavid Fekke

Summary

This article discusses the use of Dependency Injection with Express, a popular web application framework in JavaScript.

Abstract

The article begins by introducing the concept of Dependency Injection in JavaScript and how it can be beneficial when working with web application frameworks like Express. The author then proceeds to demonstrate how to use Dependency Injection in Express by breaking down the application into different modular components. The first part of the application involves creating HTTP requests using the Axios library, making these requests asynchronous. The author then moves on to discuss the Express routes, breaking the handlers up into their own module for easier testing. The service is then injected into the application using a middleware component that attaches the service object to the request object. The completed application is shown as an example, demonstrating how the service has been injected into the API routes. The article concludes by highlighting the benefits of Dependency Injection, including decoupling the application and making it more easily testable.

Bullet points

  • Dependency Injection is a useful technique when working with web application frameworks like Express.
  • The first part of the application involves creating HTTP requests using the Axios library, making these requests asynchronous.
  • Express routes can be broken down into modular components for easier testing.
  • The service is injected into the application using a middleware component that attaches the service object to the request object.
  • Dependency Injection allows for decoupling the application and making it more easily testable.

Using Dependency Injection with Express

Photo Illustration by David Fekke

Originally published at https://fek.io.

I recently did a blog post on using Dependency Injection in JavaScript. This technique comes in handy, and can be really useful when using web application frameworks like Express.

Express makes it very easy to inject dependencies into your web application through middleware or into individual routes. In my Express applications when I need a dependency, I inject it directly into a route by injecting it as a service on the request object. But before we do that, let’s break our app up into different modular components.

The Service

For the first part of this application, we will create the actual HTTP requests using the axios library. We will make these requests asynchronous.

I am using the Star Wars API for my example. I have created three functions for getVehicles, getPlanets and getStarships. These methods are using the async/await syntax, which will allow them to be used asynchronously by the module that imports these functions.

Routes

For my Express routes, I like to break the handlers up into their own module. We can then use these handlers in the actual routes, and this will make them easier to test.

As you can see from the example above, each handler can make service requests from the req parameter, and we are not importing the service explicitly into our handlers. Now we can create the routes we will make for our API calls in its own module.

The example above shows boilerplate Express route setup in this module. We can now reference this in our main express app module.

Injecting the Service

For injecting our service, we will need to create a service object that we can attach to the req object, and create a middleware component that actually injects the service into our API route.

As we can see from the example above we are now importing the individual API functions that we created using axios and creating a service factory that combines all three functions into a single service object. We then create the exposeService middleware that will actually inject this service into our route.

Our completed application should look like the following example;

In the example application, we have injected the service into our api routes so it will have access to the Star Wars API on the service property of the req parameter. We also have a default route for the index which points to the '/' route. This 'index' route does not have access to the service because we did not pass the middleware globally, we only passed it into the 'api' route.

Conclusion

Dependency injection is a very powerful tool that allows us to decouple our application. By breaking these services into their own modules, we have also made our application more easily testable using one of the many testing frameworks available to JavaScript. May the 4th be with you!

More content at plainenglish.io

Nodejs
Dependency Injection
JavaScript
Programming
Software Development
Recommended from ReadMedium