I offer the readers of Habrakhabr a translation of the article “After a year of using NodeJS in production” that I liked, by Gavin Vickery. This is a continuation of his article “Why I'm switching from Python to Node.js,” which he wrote a little over a year ago in response to his disappointment in using Python and as a justification for switching to Node.
A year of development with standard command-line tools, client projects and the release of updates for our company's products, in a nutshell, this is what I learned during this time. I would like to share my experience, but it will be not so much about Node, but for the whole JavaScript.
Easy to learn but impossible to master
Node is very easy to learn. Especially if you're familiar with javascript. Nuggle a few tutorials for beginners, play around with Express and you're already in the subject, aren't you? Then you will begin to realize that you need to somehow interact with databases. No problem, run NPM ... Yeah, just a handful of decent SQL packages. Later, you will realize that all existing ORM tools are no good and that the basic driver is the best choice. Now you have problems with the implementation of your model and verification of logic. Soon after this, you will start writing more complex queries and just get lost in callbacks. Naturally, you will read about the 'callback hell', spit on this case and start using one of the many promise libraries. From now on, you will simply begin to “massify” all the things you are doing, in exchange for a quiet weekend.
')
All this makes it necessary to say that as if the Node ecosystem is constantly moving. And the vector of this movement is not very good. New tools that seem to be “much cooler” than old ones will be met every day. Just imagine: you can always find what you have, but only also “shines”. You will be surprised how simple it can happen to you. And it seems that the community is only encouraging. Are you using Grunt !? Everyone uses gulp !? Wait a minute, use NPM native scripts!
Packages that consist of a trivial code of no more than 10 lines are downloaded thousands of times each day from NPM. Are you seriously? Do you really need to set all this dependency chain to just check the type of the array? And these same packages are used by such giants as React and Babel.
You will never become a master of what moves with such a puzzling speed. And this is not a word about the "stability" of such movements.
Error handling according to the "lucky / unlucky" principle
If you come from another language, such as Python, Ruby or PHP, you will expect something to throw an exception, something to catch it, at worst, the function will return an error. And that's fine. Completely normal ... But in the Node it is not. On the contrary, you pass errors to your callbacks or promises — that's right, no exception throws. But it works smoothly until you want to receive a call stack from several nested callbacks. Not to mention that if you forget to return a callback in case of an error, the script will continue to execute and cause a set of other errors, in addition to the first one. You will have to double the amount of debugging information to properly debug.
Even if you start “seriously” handling your own errors by the standard, you cannot (without reading the source code) make sure that most of the packages installed from NPM use the same approach.
These problems will lead you to use "catchall" exception handlers. Which will be able to secure a problematic place and allow your application to not just gracefully “fall”. Remember, Node is single-threaded. If something blocks the process, everything will fall to hell. But it's cool, you use Forever,
Upstart and Monit , right?
Callback, Promise or Generator !?
To manage the 'callback hell', bugs and hard-to-read logic, more and more developers are starting to use Promis. This is a way to write code that looks more or less synchronous, without a crazy 'callback' logic. Unfortunately, there is not a single “standard” (as for anything else in JavaScript) for implementing or using Promis.
The most frequently mentioned library now is
Bluebird . She is pretty good, works fast and makes things "just work." Anyway, I found it very useful to wrap everything I need in Promise.promisifyAll ().
For the most part, I used the wonderful
async library to keep my callbacks in check. With her, everything looked more natural.
Toward the end of my experience with Node, generators became more popular. I have not finished my “immersion” in them and therefore I can’t really say anything. I would like to hear someone who is closer to them.
Bad standardization
The last straw was that I discovered the lack of standards. It feels like everyone has their own idea of how to work with the things described above. Callback'i? Promis? Error processing? Build scripts? Yes, there is no end to it.
This is all glowing ... No one can tell how to write standardized JavaScript code. Just type in Google "JavaScript Coding Standards" and you will understand what I mean.
I understand that many languages do not have a strict structure, but they usually have a standard guideline created by language maintainers.
The only thing that is somehow acceptable is written in
Mozilla .
Node totals
I spent a year trying to get JavaScript and a more specific Node to work on our team. Unfortunately, during this time we have spent more hours searching for documentation, getting familiar with the “standards”, debating about libraries and debugging trivial code than on something useful.
Would I recommend a Node for large projects? Absolutely not. Will people use it anyway? Of course they will. I tried too.
Anyway, I would recommend javascript for frontend developers such as Angular or React (as if you have another choice).
I would also advise Node for simple back-end servers used mainly for web sockets or API provision. This can be done simply with Express, I say this, because we did this for our
Quoterobot PDF processing server. This is a single file containing 186 lines of code, including spaces and comments. And it works as well as it is simple.
Return to Python
You may well be surprised what I'm doing now? Now I continue to write the main parts of our products and APIs using Python. Mostly on Flask or Django, using either Postgres or MongoDb.
This is a time-tested option with excellent standards, libraries, easy debugging and stable operation. Of course, he has shortcomings. But he is good, one has only to start writing on it. For some reason, Node caught my eye and tightened my eyes. I do not regret my attempt to make friends with him, but I feel that I spent more time on him than I should have been.
I hope javascript and node will improve in the future. I will be happy to try it again.
Tell us about your experience? Have you had any problems that I experienced? Have you finished jumping back to a more comfortable language?