Use test-driven development to build a Node.js application

Summary

This code pattern shows you how to create a world class currency conversion microservice that was built by using test-driven development (TDD) in Node.js. This code pattern is a microservice that is a part of the Bee Travels project.

Description

This code pattern shows you how to create a world class currency conversion microservice using test-driven development (TDD) in Node.js. This code pattern is a microservice that is a part of the Bee Travels project.

Test-driven development is a style of programming that closely intertwines coding, testing, and designing. So, when designing the functionality of your application, you first write unit tests and then implement the code afterwards.

This pattern showcases modern Node.js development by using modern JavaScript ECMA script and popular NPM libraries, which are listed in the Anatomy of this Application section at the bottom of this page. For our unit tests we use Jest, a JavaScript unit-test framework testing library that works well with TDD.

Prerequisites

You need to understand test-driven development to follow the steps in this patterns.

You also need to have the following installed on your local machine:

Outcomes

After completing this code pattern, you will understand how to:

  • Design and create a Node.js microservice with a REST interface that has a swagger test harness where you can manually inspect, discover and run the various API endpoint.
  • Use and run this simple microservice.
  • Use the code base as a reference architecture and toolchain to create your own Node.js microservices.
  • Deploy and run this microservice on Kubernetes.

Flow

This flow is for the runtime of the currency conversion microservice.

flow

  1. Consumer calls the microservice over the Internet using an http/s request.
  2. The ExpressJS web server accepts the REST request (for example, GET /convertCurrency/ZAR/USD/600.66).
  3. Code routing in Express passes the request to a service module which in turn calls the European Currency Exchange API.
  4. An exchange rate for ZAR is retrieved and stored. The value of 600.66 South African Rands (ZAR) is converted to US Dollars(USD).
  5. The ExpressJS web server sends a response to the calling consumer with the dollar amount (in this case, $40.59 ).

Instructions

To try out this code pattern, see the detailed technical steps in the README.md file in the GitHub repository.

  1. Clone the repo by running git clone TDD-NodeJS-Containers.
  2. Ensure Node.js 10.16.1 or later is installed by running node -v.
  3. Install packages with NPM by running npm install.
  4. Start the app by running npm start.
  5. Browse the API from your browser localhost:4001.