November 23, 2017
Introduction to Node.js
Why use Node.js?
You may hear people saying that node.js is not good, poor performance, and not for enterprise application. Before answering this question, we better to have a look to the below pros and cons. Then, we can conclude what kind of application is good to build with node.js, and what is not.
Pros
- No need to learn new programming language, it is JavaScript
- Share the same JavaScript codes with both server – node.js and client – frontend webpage.
- Fast processing. The V8 engine, which is the runtime engine of node.js, will compile your JavaScript into native machine code.
- With build-in package management tool – NPM, you can easily add NPM modules to your node.js environment.
- With Express or other framework, you can build Restful application easily.
- Good for audio / video streaming, because node.js has fast, non-blocking and asynchronous I/O.
- Good for real-time app.
Cons
- Not good if the request is CPU intensive, because it will block the entire system.
- Not good for reporting
- Not good for calculating
Misunderstanding
- It is single-thread, can’t handle concurrency, or multi-users. NOT 100% true, node.js has one single thread listening to your request for your program.
- If the request is non-CPU intensive, this single thread will execute the job and return.
- If the request is long running I/O request, this single thread will pass to the pool of native C++ threads to handle the request.
- It is writing in JavaScript, but don’t mix up with frontend JavaScript. Node.js is for building backend server, not frontend.
- Too many callbacks. Because asynchronous is a nature of Node.js. However, most of our applications’ functions may need to be synchronous, especially interacting with users. So, we may need to define our function with callback, in order to make it to be synchronous.
So, we can conclude:
Good for | Bad for |
non-CPU intensive | CPU intensive |
Simple web page | Generate reports |
Restful API | Intensive calculation |
I/O streaming | CRUD application |
Socket development |
You can follow this blog in order to buildup some examples with node.js.