Nodejs is the attempt to unify all the technologies of the web. With it, you can write one language to rule them all. JavaScript on the front end and JavaScript on the back end. The beauty of it, is that you don't have to worry about the one thing that makes JavaScript a pain to work with on different browser: The DOM.
On the back end, you have pure speed, no none sense. Like any respectable environment, Nodejs offers the full power of a scripting language right from start. I was very tempted to convert my entire blog to node, but every time I face the same hurdle.
I am foremost a front end developer, so I have spent a lot of time with JavaScript in my career. I have no problem dealing with call backs or other things people hate about JavaScript. To me, it is just the way things are done. However, the problems I face with Node are not your traditional ones. At least not something most developers like to talk about.
- single threaded application
- Web server
Every few months I get excited by Node and forget why I failed to convert my framework again. There are very few useful tutorials when it comes to node, because a lot of people limit themselves to HelloWorld.
This blog runs on PHP and Apache. You get quite a few advantage by default with those two being tied together. One of my favorite is you don't have to worry about serving files. Also, Apache runs many parallel instances of PHP so it can handle multiple requests.
With Node, you run a single process, that runs your code and serves your files. That seems cool in the beginning because you don't have to worry about writing complex config files like with your traditional web server. But it becomes a short coming as soon as you get any decent amount of traffic on your website.
I was lured into nodejs because of its real time communication feature. I made a chatting app like everyone else. It worked fine on my local machine at work, at first. As soon as I gave the link to more than 10 people, I noticed it started to lag. Though I have to admit my machine was running multiple other services and applications at the same time.
Node runs your code in a single process, this means if you have a CPU intensive task, it will affect all your users. This is not necessarily a bad thing because it forces you to reorganize your application. The CPU intensive or slow IO stuff shouldn't block the rest of your application. Actually, the power of nodejs lies in its Asynchronous IO capability.
With a server like Apache or Nginx it is easy to forget all about serving files. You almost never have to do anything to serve your file. With nodejs since you create your web server in node, you have to handle your files by yourself.
Frameworks like expressjs can take care of it for you, but still it can be intensive to have your server run both the code and serve the files. So my suggestion is to have an instance of Nginx serve all your static file. You will thank me when your server suddenly gets a million visitors.
To summarize:
- Your code runs in a single thread. It makes sense to separate all your CPU intensive tasks into a different process.
- Since node is the web server, it makes more sense to use another server for your files. This helps you separate static files from code.
These are things we don't always thing about but they can greatly impact your application. Node allows you to create very fast applications in JavaScript in all ends. Hopefully, you can take these things into consideration before you get started.
Comments
There are no comments added yet.
Let's hear your thoughts