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