Node.js Flashcards
What is Node.js? Where can you use it? (3)
open-source, cross platform JavaScript runtime environment and library to run applications outside the client’s browser
used to create server-side applications
great for data-intensive applications as it uses an asynchronous, event-driven model (but not for computationally intensive applications)
Why use Node.js? (4)
generally fast
rarely blocks
allows you to use JS across the stack
everything is asynchronous
yields great concurrency
How does Node.js work? (6)
- Client sends request to the web server.
- Node.js retrieves the incoming requests and adds those to the Event Queue
- Requests are then passed one by one through the Event Loop if they don’t require any external resources
- The Event loop processes simple requests (non-blocking operations) and returns the responses to the corresponding clients.
A single thread is assigned to a single complex request which is responsible for completing a particular blocking request by accessing external resources (eg. database, file system, etc.)
Once the task is complete, the response is sent to the Event Loop and then back to the client.
Why is Node.js single-threaded?
Node.js is single-threaded for async processing which yields better performance and scalability compared to a typical thread-based implementation.
How does Node.js handle concurrency?
Node adheres to the single-threaded event loop model (similar to the JS event-based model and callback system) so it can manage more concurrent client requests.
Explain a callback in Node.js
A callback function is called after a given task. It allows other code to be run in the meantime and prevents any blocked. Node relies heavily on callbacks (all APIs are written to support callbacks)
What are the advantages of using promises instead of callbacks? (4)
control flow of async logic is more specified and structured
coupling is low
built-in error handling
improved readability
How would you define the term I/O?
The term I/O is used to describe any program, operation, or device that transfers data from one medium to another (could be a physical device, network, or files within a system)
How is Node.js most frequently used?
SPAs real-time chats real-time collaboration tools microservices architecture streaming applications
What is NPM?
Node Package Manager - responsible for managing all the packages and modules for Node.js
Online repo for all packages
Command-line utility to install/update/uninstall dependencies
What are modules in Node.js? (3)
Like JS libraries that can be used in a Node application to include a set of functions.
To include a module, you use the require() function
There are a lot of built in modules to provide the basic functionality needed for a web application (eg. file system, url parsing, streaming data, utility functions)
What is the purpose of module.exports?
a module encapsulates all related code into a single unit of code that can be parsed by moving them into a single file
What database is more popularly used with Node.js?
MongoDB (NoSQL) - document-oriented database that provides high performance, availability and easy scalability, PostgreSQL (SQL)
What are some of the most commonly used libraries in Node.js?
ExpressJS - flexible web app framework that provides a wide set of features to develop web and mobile apps (Hapi, Koa)
Mongoose - makes it easy to connect an application to a database
Pros of Node.js (4)
fast processing and event-based model
uses JS - well known language
NPM has over 50K packages
best suited for streaming huge amounts of data and I/O intensive operations
Cons of Node.js (4)
Not suitable for heavy computational tasks or CPU intensive tasks (because it is single-threaded)
using callback is complex since you end up with several nested callbacks - can end up with callback hell
What is the command to import external libraries?
require()
What does event-driven programming mean?
uses events to trigger various functions - eg. a key or clicking a mouse button
What is an Event Loop in Node.js?
The Event Loop handles async callbacks and is the foundation of the non-blocking input/output in Node
Differentiate between process.nextTick() and setImmediate()
nextTick() postpones the execution of an action until the next pass around the event loop or calls the callback function once the event loop’s current execution is complete
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 an EventEmitter in Node.js?
EventEmitter is a class that holds all the objects that can emit events
When an object from the EventEmitter class throws an event, all attached functions are called upon synchronously
What are the two types of API functions in Node.js?
async, non-blocking functions
sync, blocking functions
What is the package.json file?
The package.json file is the heart of a Node.js system - holds the metadata for a particular project and can be found in the root directory of a Node application or module
How would you use a URL module in Node.js?
Provides various utilities for url resolution and parsing to help split up web addresses into a readable format
How do you create a simple Express.js application?
Have request and response objects - using an MVC pattern, configure endpoints, and manage middleware with routers and controllers invoking the next method to continue down the chain of responsibility
What are streams in Node.js
Objects that enable you to read or write data continuously (types of streams: readable, writeable, duplex, and transform)
How do you install, update, and delete a dependency?
npm install
npm update
npm uninstall
How do you create a simple server in Node.js that return Hello World?
import HTTP module
use createServer function with a callback function using request and response as parameters
type “hello world”
set server to listen to a port 8080
Explain asynchronous and non-blocking APIs in Node.js
All Node.js APIs are async meaning they are also non-blocking - the server never waits for an API to return data, just moves to the next API after calling it and a notification mechanism from a Node.js event responds to the server for the previous API call
What is REPL in Node.js
stands for Read, Eval, Print, Loop and represents a computer environment
similar to a windows console or unix/linux shell in which a command is entered and the system responds with an output
What is the control flow function?
a piece of code that runs between several async function calls:
- controls the order of execution
- collects data
- limits concurrency
- call the next step in a program