Sails - Js Essentials - Sample Chapter
Sails - Js Essentials - Sample Chapter
Sails - Js Essentials - Sample Chapter
ee
P U B L I S H I N G
C o m m u n i t y
E x p e r i e n c e
D i s t i l l e d
Sails.js Essentials
$ 29.99 US
19.99 UK
Shaikh Shahid
pl
Sails.js Essentials
Sails.js Essentials
Sa
m
Shaikh Shahid
experience of working on Node.js for more than two years. He loves to spread the
word about Node.js and its various packages via his programming blog. Shahid
is also very interested in software architecture and design and loves to develop
software system from the core.
When he is not playing with Node.js or helping people, he watches movies, reads
books, or travels to Goa.
Preface
Sails.js Essentials will take you through the basics of Node.js and developing
production-ready application in the Sails.js framework. This book covers interesting
application and their development that will guide you through the practical aspects
of software and development.
Preface
Node.js architecture
[1]
Node.js architecture
We all know that Node.js runs on top of V8Chrome runtime enginethat compiles
the JavaScript code in the native machine code (one of the reasons why Google
Chrome runs fast and consumes a lot of memory), followed by the custom C++
codethe original version has 8,000 lines of code (LOC)and then, the standard
libraries for programmers. The following is the figure of Node.js architecture:
JavaScript
Standard Libraries
Binding Libraries
V8
libeio
libev
libuv
DNS
-Architecture
V8
The V8 JavaScript engine is an open source JavaScript engine developed for the
Chrome project. The innovation behind V8 is that it compiles the JavaScript code
in native machine code and executes it. The developers used the just-in-time (JIT)
compiler methodology to improve the code compilation time. It is open source and is
used in the Node.js and MongoDB project.
[2]
Chapter 1
HTTP
Thread
File O/P
Thread
libuv
O.S.
DNS
Thread
Request
callback
Working of libuv
When you make an HTTP request to web server running over Node.js. It creates
the libuv thread and is ready to accept another request. As soon as the events are
triggered by libuv, it returns the response to user.
The libuv library provides the following important core features:
Thread pool
[3]
Child process
Signal handling
The libuv library internally uses another famous library called libeio, which
is designed for threading and asynchronous I/O events and libev, which is
a high-performance event loop. Therefore, you can treat libuv as a package
wrapper for both of them.
Thread Pool
Request
Request
Blocking IO
Request
Thread Processing
Threaded Waiting
[4]
Chapter 1
Request
Event
Loop
Delegate
Request
Request
POSIX
Async
Threads
NonBlocking IO
Single
Thread
Thread Processing
Threaded Waiting
If the single-threading programs work correctly, they will never block the I/O and
will be always ready to accept new connections and process them.
[5]
Node
Runtime
Stack
Event loop
Queue
(Event loop)
The libuv library creates the thread and returns the callback to us. As it's an
asynchronous operation, it goes to queue instead of the stack and the event loop
fetches it when the stack is empty and does the execution.
You can validate the same concept using the setTimeout() function.
Consider the following code:
console.log("i am first");
setTimeout(function timeout() {
console.log("i am second");
}, 5000);
console.log("i am third");
[6]
Chapter 1
If you run the previous code, you will get an output similar to the following:
i am first
i am third
i am second
The reason is obvious, setTimeout() waits for five seconds and prints its output;
however, that does not block the event loop.
Let's set the timer to 0 seconds and see what happens:
console.log("i am first");
setTimeout(function timeout() {
console.log("i am second");
}, 0);
console.log("i am third");
Why so? Even if you set the timer to 0, it goes in the queue; however, it is
immediately processed as its time is 0 second. The event loop recognizes that the
stack is still not empty, that is, third console was in process; therefore, it pushes the
callback after the next tick of event loop.
Summary
In this chapter, we discussed the architecture of Node.js, followed by its internal
components: V8 and libuv. We also covered the working of event loop and how
Node.js manages the performance improvement using single-thread processing.
In the next chapter, we will take a look at the development of the Node.js server
using core modules as well as the Express web framework.
[7]
www.PacktPub.com
Stay Connected: