
This video will give you an overview about the course.
Most Node.js developers know that Node.js is a single threaded non-blocking I/O platform.
Present what Node.JS consists of
Show examples of the event loop along with stack, heap, and code
The event loop
Promises are an improvement to the callback hell but they’re not always used in an optimal way.
Improve existing callback code with promises
Demonstrate optimal error handling
Mastering promises
Promises and async/await are here to stay but some of the internal libraries like fs still use callbacks.
Use Util.promisify to promisify fs.readFile
Read from a file using both the callback approach and the promise approach
Using Util.promisify
Async/await is the latest development in the JavaScript world and a complete solution to callbacks.
Transform the code files from the previous video to files using async/await
Demonstrate proper error handling with try/catch
Learning async/await
Traditional imperative programming can only take us so far. A modern JavaScript developer that can take full advantage of Node.js 11 has to know a bit about functional programming.
Describe the paradigm
Describe pure functions
Show second example with async call
The best way to introduce an intermediate dev to the concepts of functional programming is through the inversion of control that happens when you switch from a typical for loop to .forEach.
Create a list of products that we need to compute the average price for
Write impure code with typical for loops
Replace the impure part with .forEach
What if we want to pick some of the array contents out of the array? It’s better to use .filter instead of for loops.
Demonstrate conventional solution
Replace with .filter
We need to make sure that the function computing the average leaves no side effects. Map and Reduce are also very useful assets in the functional toolkit.
Step by step example in Quokka
Copy over to app.js
Add a call to reduce
RxJS is a difficult to understand library.
Start with explaining observables
Explain marble diagrams
Focus on describing .buffer()
Time to work with some RxJS code.
Create an observable that emits data every 1500ms
Subscribe to it
Use .pipe with .map, .filter and .buffer(interval)
Express is often misused with multiple endpoint implementations flooding the main file.
Introduce the concept of controllers in the context of Express.js
Create a PlansController and a SubscriptionController
Register them via express router with the main application
Controllers should not do all the work as is often the case. So, in this video we need to introduce services.
Create a Plans Service
Create a Subscriptions Service
Middleware is essential to a well-operating application.
Install some common middleware
Create an appropriate directory structure
Work with doting
We need a strict model validator that does not interfere a lot with the rest of the code.
Install Joi
Create model validators
Use the validators in the controllers
Reporting errors is also essential. We need to take advantage of the async wrapper.
Create a list of error classes
Catch the errors in the wrapper
Forward the errors in a series of global error handling middleware
We need a quick way to spawn up a database.
Download and install Docker
Create a new MySQL container
We need a CLI to connect to our new database.
Install Sequelize CLI
Prepare workspace
We need to create model definitions but we already use the files for validation.
Copy existing model validations
Generate model definitions using --force
Paste model validations on the same files
Migrations are a powerful code first tool.
Setup Sequelize migrations
Execute migrations
Check the DB for the newly created tables
We now have an ORM so it’s time to use it to implement the services we’ve left empty
Implement the PlansService
Implement the SubscriptionsService
In contrast with traditional applications, microservices need to have a single database per service.
Use Docker to create two databases
Connect with MySQL Workbench
Create schemas in both databases
It’s time to begin splitting the monolith into two microservices.
Create the plans-service
Create the subscriptions-service
Install and run PM2
The easiest communication protocol between microservices is the REST protocol.
Install axios to subscriptions-service
Make a call with axios from subscriptions-service to plans-service
AMQP has made its name with microservices. It’s imperative for anyone claiming to work with microservices to understand this important protocol.
Create an account and work with CloudAMQP
Install amqplib and create a PaymentsService
Send messages down the queue from the subscriptions-service
CQRS and event sourcing are very advanced concepts that, even though they won’t be used in the course, the viewer needs to be familiar with them.
Explain CQRS
Explain event sourcing coupled with CQRS
API gateways are important for hiding the internal, usually complex, structure of microservices.
Install express gateway
Create a new gateway project
Map the services to endpoints
The first step when creating an authentication service is to define how a user is stored in the persistence layer.
Create a new database with Docker
Initialize a sequelize project
Create user model + validation schema
Proper password hashing is extremely important when implementing an authentication service.
Create a PasswordHasher class
Implement a hash method
Implement a check method
We want to limit load to our authentication microservice so we need a self-contained token.
Introduce JWTs
Install json web token
Create a user’s-service class and implement jwt functionality
The primary controller in the service consists of two endpoints, one for signing in and one for sign-up.
Create a UsersController
Register the controller with index
Call users service in the controller
The sign in endpoint remains for finishing the service.
Implement the sign in method in the service
Move it to the controller
Register all envs
As of now, we have a working authentication service but no way of identifying users in the rest of the services.
Describe passport
Install passport
Install passport-jwt on both services
To finish authenticating the users without needing to query the actual auth-service we need authentication middleware.
Implement a protected-route middleware
Add it to both controllers
Register passport on both plans and subscriptions microservices
Some users may not be familiar with Redis so they need a brief introduction.
Introduce Redis as a concept
Demonstrate in a slide how we are going to use Redis
The fastest way to get started with Redis is via a Docker container.
Create a Docker container
Connect with redis-cli
Execute some commands
We need to create a caching service class to support the caching functionality in the plans microservice.
Create a new class
Implement the getPlans method
Implement the store and purge methods
Right before write operations the cache needs to be invalidated.
Call purge on every write operation
Start the project
Demonstrate the caching service in action
Node.js is a technology that you can quickly become familiar with, but it can be hard to master to build fast and secure solutions. Instead of covering the basics, this course will help you to progress to understand advanced concepts such as asynchronicity, the event loop, clean error handling with Express Middleware and functional reactive programming.
You will start off by working through the process of creating a subscription management system, where you will optimize directory structures. This will help you with the organization, as Node.js apps usually don't follow filename and directory conventions. As you delve deeper into the course, you'll learn how to build an API in the form of microservices, managed through PM2. As you progress, you will connect and utilize two of the most widely used technologies in the Node.js World; MySQL, the most commonly used relational database, and Redis, a popular technology used in caching. Once you've built a solid infrastructure, you'll be able to build an authentication service with the help of Passport and JSON Web Tokens.
By the end of this course, you will be able to tackle harder challenges in your Node.js projects by efficiently using the best tools available, while also meeting demanding industry standards for a clean and scalable code.
This course uses Node.js 12.0.0, while not the latest version available, it provides relevant and informative content for legacy users of Node.js.
About the Author
Dimitris Loukas is a software engineer currently part of a growing start-up building a revolutionary referral marketing platform with Node.js, Angular, MySQL, Redis, and Docker. He is also developing trading software applications using C#, Aurelia, and TypeScript. He is an author at Packt and has developed many video tutorials in C#, .NET Core, and TypeScript. He has worked for two start-ups in the past, is active in the open-source community, and loves taking up small side projects. He has developed microservices using C# and Node.js and has also mastered Angular, Aurelia, and Vue.js. He was an early adopter of .NET Core and is fascinated by modern JavaScript and where the web is going.