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.