In this section we deal with how Javascritp handles files both synchronously and asynchronously. The following topics will get involved.
- Binary Data, Character Sets, and Encodings
- Files and fs
V8(which is embedded in the
Node) is synchronous, it can only handle one thing at one time. However, the entire process of
Node is asynchronous, because there are things happening in
V8 at the same time and all of these are inside
When we are talking about
event driven part means we are asking for
I/Os to happen, and we get an event notification when they are done (
libuv‘s event loop is constantly checking for the events in its queue and calls their callback methods). Those
Node will continue to do something else without waiting the current task to finish, we say this process is
libuv has its own website: http://libuv.org/ .
Data stored in Binary (sets of 1s and 0s).
A representation of characters as numbers. Each character gets a number. Unicode and ASCII are character sets. e.g. “hello” can be represented in Unicode as h (104) e (101) l (108) l (108) o (111). A character set decides what number and which number can be assigned to each character.
How characters are stored in binary, especially how many bits are used to represent a number in memory, e.g. UTF-8.
When we are talking about character set, we are talking about what number is assigned to a character. When we are talking about encoding, we are talking about how many bits we are using to store that number.
Buffer in Node is global. It holds raw binary data and it let us switch those binary data into strings according to the specified encoding methods.
A function passed to some other function, which we assume will be invoked at some point. The function ‘calls back’ back invoking the function you give it when it is done doing its work.
Synchronous way to read a file. It is useful when reading some configuration files which have be loaded before we can continue.
Asynchronous way to read a file which returns a buffer in memory. It is a good approach to read some large files since it won’t block the codes from running. However, if the file is super big or we don’t want to put all the content in the memory, we should use the streaming way instead.
Error-First Callback: Callbacks take an error object as their first parameter. null if no error, otherwise will contain an object defining the error.
A stream is just a sequence of pieces of data. The data is broken up into chunks.
Example about creating a readable stream.
Examples about creating a writable stream.
We have a smarter way to deal with copying contents from one stream to another stream than the snippet above. That is pipe.
Connecting two streams by writing to one stream what is being read from another. In Node, we can pipe from a Readable stream to a Writable stream.
A simple example about pipes between two files.
The following example shows how to use pipe to create a compressed file.
.apply() are helper methods for calling a method, while passing a variable that the object’s
this keyword will point to.
For example, we have the following three ways to invoke the
greet() mtheod. The difference between
.apply is that,
.call passes in the parameters one by one, while
.apply passes the parameters that grouped in an array.