NodeJS Flashcards
What is the difference between NPM and NVM?
Node Package Manager
- Shipped with NodeJS
- CLI to manage remote node modules
- Allows you to publish, download and update modules
- Stores remote modules in node_modules folder
Node Version Manager (NVM)
- Allows you to switch between multiple versions of Node - Recommended way to install Node
What is the difference between a node module and a node package?
Module
Encapsulates all related code into a single file
Package
One or more modules
What is Node.js and where can you use it?
JavaScript runtime environment and library
Uses to create server-side applications
Use cases:
- Data-intensive apps - uses the asynchronous, event-driven model
- I/O intensive web apps (video streaming)
- Realtime web apps
- Microservices
What are the pros and cons of Node.js?
Pros
- Low latency and high throuput due to its non-blocking approach
- Everything is asynchronous
- Great concurrency
Cons
- Not suitable for heavy computational tasks
- Not suitable for CPU-intensive tasks since it is single-threaded
How does Node.js work?
Runs on a single process with requests being processed on a single thread
When a request is sent, Node.js places it in an event queue and uses the Event Loop to listen for async events
If a request is non-blocking, the request is immediately sent back to the client
If a request is blocking, request is sent to the worker thread pool and when it’s finished, it will be sent back to the Event Loop to be sent back to the client
Why is Node.js single-threaded?
More performance and scalability can be achieved by doing async processing on a single thread instead of typical thread-based implementation
How does Node.js handle concurrency?
Node.js adheres to the Single Threaded Event Loop Model
Aysnc calls are handled by the libuv library which sets up a thread pool to handle concurrent requests
What is the Event Loop in Node.js?
Listens for async events which gets actioned by event handlers
Foundation of non-blocking I/O
EventEmitter => events => Event Loop => event handlers
What is a callback?
A function that’s passed into another function as a parameter and only runs after its parent function has finished executing
Uses nested functions to ensure async tasks are handled in the desired order - can easily lead to callback hell
What are the advantages of using promises instead of callbacks?
- The flow of async logic is more specified and structured
- Low coupling
- Built-in error handling
- Improved readability
What is an EventEmitter?
Module that facilitates communication between objects in Node.js
Consists of 2 main features:
- Emitting name events
- Registering/unregistering listener functions
Whenever an object from the EventEmitter class throws an event, all registered listeners are called synchronously - ensures proper sequencing of events and avoids race conditions
As best practice, listeners should always be added for “error” events
What is Express.js?
Node.js web application framework
What is the difference between process.nextTick() vs process.setImmediate()?
process. nextTick()
- Postpones execution of action until the next pass around the event loop and calls the cb function once event loop’s current execution is complete
process. setImmediate()
- Executes a callback on the next cycle of the event loop and returns control to the event loop for any I/O operations
What is the REPL?
Read Eval Print Loop
Represents a computer environment
Read - read user’s input; parse and store in memory
Eval - evaluates data structure
Print - print result
Loop - loops through commands until user terminates it
What is piping?
A mechanism to connect the output of one stream to another stream
Used to retrieve data from one stream and pass output to another stream
What is callback hell?
Result of intensively nested, unreadable and unmanageable callbacks
Improper implementation of async logic causes callback hell
What is middleware?
Functions that sit between the route request and route response
Once the function has finished executing, it can either finish the request-response cycle or invoke the next middleware in the stack
ie. authentication, transforming request, logging
What does I/O mean?
Input/Output
Accesses anything outside your application - loaded into the machine memory to run the program once the machine has started
What is the difference between Node.js, AJAX and JQuery?
Node.js
JavaScript runtime to write backend applications
AJAX
Technology used to send requests to web servers and retrieve data from them without reloading the page
JQuery
JavaScript library that helps with frontend development