There are many different tutorials about Docker and a huge amount of detailed documentation, but you might need to go through tons of them to understand why docker is needed, and how to use it. Even after that, you probably don’t get a whole picture. In this part, I aggregate all the key information required to understand what is docker, why you need it, which role it plays in development and deployment, and how it can simplify a lot of things for you.

Theory

What is docker?

What if you need to install an operating system and a list of software to multiple identical personal computers and pre-configure it? …


Image for post
Image for post

A few years back, I was managing iOS client team, and one of my engineers integrated functional testing. We used Appium, and that was a hassle. It could take hours to make it working on a new developer’s machine or a new node for builds and tests. The number of steps you had to go through to set it up killed any enthusiasm, and it didn’t work in the end. Damn it…

Later, I started using React Native for all mobile development with a business logic. As of functional testing, I use detox for testing all React Native code, and I use WebDriverIO for testing which goes out of React Native functionality, for example to test “create an account and login flow with FB/Twitter/Google”. …


Clear project folders and files organization in the first but important step on a way to efficient, reliable, maintainable, extensible code, or in short, clean code. When you have 3–4 developers working on a project, it is easy to keep it clean even without any formal rules. When a team begins to grow, you need to keep an eye on it, probably even document it, otherwise you are at risk to have a trash can soon.

In this appendix, there’s an overview of the project sources, Backend API Server with Node.js, used in the tutorial. …


Image for post
Image for post
Minsk, Belarus, by my classmate and friend Flyer

A couple of years ago, having had a solid engineering background, over 10 years of C/C++ for *nix systems and 5 years for iOS Native, I needed to develop a business logic application for iOS and Android and a backend for it in 3 months only. I chose React Native for a mobile development, and Node.js for a backend, both on 2 main reasons:

  • The same language, TypeScript (JavaScript), and the same set of tools and libraries for a client and a backend;
  • The largest software development community, the JS community, leading to the largest and great set of tools and libraries. …


Image for post
Image for post

In the previous part of the tutorial, we reviewed internal vs external caching, and different caching techniques. In this part, we’ll try external caching with redis, learn how to use it in the code, run redis server in a development environment with docker, and mock it for unit tests.

If you don’t follow the tutorial, you can get the sources and use them as a start point:

$ git clone https://github.com/losikov/api-example.git
$ cd api-example
$ git checkout tags/v7.0.0
$ yarn install

Run Redis Server with Docker

The easiest way to run redis on local machine is to use docker. We already have ./scripts/run_dev_dbs.sh


Based on the needs of your Node.js application and your deployment infrastructure you might need to use internal or external caching. Internal cache stores objects inside your Node.js app, and they are available only to it. While external cache is deployed as a separate instance and available to any Node.js app and used when scaling. Redis and Memcached are the most popular choices as external caching systems. Internal and External cache can be used together. Consider all tradeoffs when making decisions.

Image for post
Image for post
Caching: Internal, External, Combined

In this part, we’ll review one of the modules for internal caching, node-cache. First, let’s review caching techniques.

Caching Techniques

Write-through — writes both to a cache and to a database, and only when both are completed, returns the competition result to a user. It doesn’t really matter if it writes in parallel or sequentially, synchronously or asynchronously (always do asynchronously for Node.js); the main principle if return competition state only after it is written to the cache and the DB. …


Image for post
Image for post
JWT (JSON Web Token) in action

In this part of the tutorial, we’ll integrate JSON Web Token, or JWT, to our Node.js app and use it for user authentication. On the diagram above, I outlined the main use case for using JWT. User authenticates and receives a JWT. Only Auth Service has a Private Key, and uses it to create a JWT with a jwt.sign(), which will include any payload (userId in our case). jwt.sign() also sets up a JWT expiration time. No backend services are required to persist JWT; it is a client’s responsibility. Any other service, when it receives a request from a user with a JWT, verifies the JWT with jwt.verify(), and extracts the payload (userId) using a Public Key.


Image for post
Image for post
San Diego Zoo, Freedom Out There

In this part of the tutorial, we’ll learn how to work with MongoDB using mongoose. We’ll follow the best practices and develop our backend together with unit tests using jest. In the end, we’ll run the server with a real MongoDB using docker, and try the API calls using curl, to make sure it works. We’ll work on a single API call to register/create a user, but we’ll add more code now which we’ll use in the next parts.

$ git clone https://github.com/losikov/api-example.git
$ cd api-example
$ git checkout tags/v4.0.0
$ yarn install

Mongoose, Initial Setup

Let’s install dependencies first:

  • mongoose — mongodb object modeling library for node.js; …


It is not possible to develop an application efficiently without Unit Tests. This statement is more than the truth about HTTP API server. Literally speaking, I never run the server when I develop new functionality, but develop Unit Tests together with the code, and run Unit Tests to verify the code, step by step until I finish the functionality.

In previous parts of the tutorial, we developed a Node.js+Express+Open API App with GET /hello and GET /goodbye requests. If you start from this part, you can get it by:

$ git clone https://github.com/losikov/api-example.git
$ cd api-example
$ git checkout tags/v3.3.0


Image for post
Image for post

In the previous part of the tutorial, we implemented a HTTP API Web server. In this part we’ll add some more features required during development and in production: fix relative import paths to absolute, add loggers to express, customize logger level based on environment variables.

If you start from this part, you can get the sorces to start from:

$ git clone https://github.com/losikov/api-example.git
$ cd api-example
$ git checkout tags/v2.2.0
$ yarn install

TypeScript Absolute Imports Paths

There’s no a standard way how to make it work, and I often see different approaches. I use tsconfig-paths in my Node.js and ReactNative projects. …

Alex Losikov

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store