Node Flashcards
process
Node’s global object containing information about the current process and some methods.
pocesss.env
Information about the environment in which the process is currently running.
process.argv
An array of command line values provided when the current process was initiated.
process. argv[0] - the absolute path to Node which ran the process
process. argv[1] - the path to the file that’s running
process. argv[2 -> …] - command line arguments provided when the process was initiated.
req
, res
Objects with properties and methods that we can use to customise our HTTP requests.
req
- the object which contains information about the client HTTP request
- an instance of IncomingMessage
res
- provides methods for sending back the server HTTP response
- an instance of ServerResponse, which is a writeable stream
Main response time cost
Usually the SUM of time it takes to execute ALL db queries.
In Node, you can execute all the db queries IN PARALLEL, reducing the response time to that of the slowest db query.
What is Node?
- an synchronous event driven runtime, or environment, that allows for execution of JS outside of the browser
- a library (comes with a lot of moudles)
package.json
Project’s control file
Are expensive operations OK in Node?
Yes, al long as you make sure not to block the Node.js process with them.
You could spawn a child process (child_process.fork()) and take advantage of multiple cores in your environment.
How can you use multiple threads in Node?
You could spawn a child process (child process.fork()) and take advantage of multiple cores in your environment, even though Node was designed without threads.
If you need more instances of the event loop, you can create while them making sure that global data in each instance isn’t shared. If your instances need to collaborate, your need some communication mechanism.
Event loop as a runtime construct
Node presents an event loop as a RUNTIME CONSTRUCT instead of as a LIBRARY.
There is no start-the-event loop call:
- Node simply enters it after executing the input script, and
- Exits it when there are no tasks callbacks left to perform
This behaviour is like the browser JS where the event loop is hidden from the user.
Blocking vs. Non-blocking calls
BLOCKING
- execute synchronously
- extremely cheap synchronous operations (e.g., console.log) don’t cause problems, but they can still stall the Node’s event loop, given that there are enough of them within one loop iteration.
NON-BLOCKING
- execute asynchronously
The most important rule for writing Node apps
Don’t block the event loop:
- handle I/O intensive operations asynchronously
- keep your code (everything that happens synchronously within one loop iteration) lean
How does Node work?
Node
- Starts
- Loads our app (into the event loop, it is already there)
- Loops until there’s nothing left to do
- Then our app terminates
In the case of an HTTP server, we create a background process that CONTINUOUSLY WAITS for requests from clients, so the app doesn’t terminate when there are no callbacks left to process.
What is performed within one loop iteration?
All the synchronous operations given. Async operations are left for future loop iterations.
Why are slow blocking operations a global problem in Node.
Because there is only one process, running on a single CPU core.
A slow DB query in Python would result in a slow page load for one particular user, while in Node that would be a global problem affecting all users.
Parallelism in Node
Everything runs in parallel, except your code.
Your code is the king, while Node is its army of servants which perform their tasks in parallel, but only report to king one at a time.
There can be only one callback firing at the same time. All the other callbacks have to wait for the first one to finish executing.
One benefit of such single-threaded/event loop design is that it eliminates the worry that different pieces of code will access the same data structure at the same time.
What is the execution/parallelism model of Node?
The single-threaded event loop.
What does Node do when you call an async function?
It starts an operation outside the event loop.
Conceptually, Node starts an async operation and makes a mental note that when this operations triggers an event, the callback that was passed to the operation needs to be called.
How does a Node server work
- Node runs a single-threaded event loop registered with the system to handle connections.
- Each new connection causes a system callback to fire
- The callbacks handle requests with non-blocking I/O calls
- If necessary, callbacks spawn threads from a pool to execute blocking or CPU intensive operations, and to load-balance across CPU cores
Benefits of the Node’s single-threaded event loop execution model
- Node’s approach to scaling with callbacks requires less memory to handle more connections than most architectures that scale with threads.
- The single-threaded/event loop design eliminates the worry that different pieces of code will access the same data structure at the same time.
fs
Provides simple wrappers around standard POSIX file I/O functions.
The Event Emitter Pattern
Useful in situations when more than one callback event must be handled.
It’s a function that returns an Evt Emitter object which is a writable stream.
Using this object’s .on method we can attach callbacks to different events.
The Event Emitter object
A writable stream that can be used to attach callbacks to different types of events.
EvtEmitter.on(‘error’, …)
Will raise an exception if not handled by a callback.
EvtEmitter.on(‘end’, …)
Fired once, and from then onward no other events will be fired from the evt emitter.
EvtEmitter.once(‘evtName’, cbName)
Like the ‘.on’ method, but its event callbacks fire only once, on the first occurrence of the event of the type, and then are removed from the list of event listeners.
Detach callbacks from EE’s listeners
ee. removeListener(‘evtName’, cbName)
ee. removeAllEventListeners(‘evtName’)
When should you execute async operations in parallel in Node
When they don’t inherently depend on each other (one does not itself need the result of another operation), even when you need the results of both operations.
If you wait for each operation to finish before proceeding with the next one, main response time cost would be the sum of all times it took to perform each given operation. If you execute them in parallel, the main response time cost is that of the slowest of the operation.
How to perform async operations in parallel
async.parallel
How to perform async operations in series
async.series
How to perform async operations in series, so that the subsequent operations get results of the ones preceding them
async.waterfall
Core Node.js modules
fs http path url querystring util