An aspiring software craftsman journey, By Mahmoud Ben Hassine
So I decided to give it a try by building ChessHub.io with a completely new technology stack and way of thinking:
That was a real challenge since everything was new to me and I had to learn all these technologies at the same time. Challenge accepted
In this post, I will share my experience on building ChessHub.io and I will try to summarize some of the mistakes I made.
First of all, let me begin with the positive parts:
That said, a stack like Node, Express and Mongo is not suitable for every application and is not intended to replace the good old Java app server backed by a relational database. Here are some use cases where Node JS is a perfect choice:
In contrast, Node is not suitable for CPU intensive apps since it is single threaded. Don’t use Node to calculate the nth fibonacci number! In the same way, MongoDB is not suitable for apps that require transactions across multiple collections, in such cases, you can keep the RDBMS you love.
One of the mistakes I made was to think synchronous using Node JS. Let me show you an example. In ChessHub.io, I want to show a random quote from a chess master on the home page, so here is a pseudo code I wrote in the very first stage:
This is actually a blocking snippet since it will wait the database to respond with the quote to be able to continue to the next instruction. When you spend several years thinking synchronous, it is natural to make such a mistake. After investigation , I realized that this way of thinking when using Node is completely wrong. A asynchronous version of this snippet is:
In this configuration, if the database take some time to load data, Node can continue serving other requests and when the data is ready, the callback function will be called asynchronously.
That was my retrospective on building ChessHub.io. If you got the same experience, do not hesitate to share your thoughts in comments.
See you soon