Node.js Flashcards

1
Q

What is Node.js? Where can you use it? (3)

A

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)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Why use Node.js? (4)

A

generally fast

rarely blocks

allows you to use JS across the stack

everything is asynchronous
yields great concurrency

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

How does Node.js work? (6)

A
  1. Client sends request to the web server.
  2. Node.js retrieves the incoming requests and adds those to the Event Queue
  3. Requests are then passed one by one through the Event Loop if they don’t require any external resources
  4. 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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Why is Node.js single-threaded?

A

Node.js is single-threaded for async processing which yields better performance and scalability compared to a typical thread-based implementation.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

How does Node.js handle concurrency?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Explain a callback in Node.js

A

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)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What are the advantages of using promises instead of callbacks? (4)

A

control flow of async logic is more specified and structured

coupling is low

built-in error handling

improved readability

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

How would you define the term I/O?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

How is Node.js most frequently used?

A
SPAs
real-time chats
real-time collaboration tools
microservices architecture
streaming applications
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is NPM?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What are modules in Node.js? (3)

A

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)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What is the purpose of module.exports?

A

a module encapsulates all related code into a single unit of code that can be parsed by moving them into a single file

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What database is more popularly used with Node.js?

A

MongoDB (NoSQL) - document-oriented database that provides high performance, availability and easy scalability, PostgreSQL (SQL)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What are some of the most commonly used libraries in Node.js?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Pros of Node.js (4)

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Cons of Node.js (4)

A

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

17
Q

What is the command to import external libraries?

A

require()

18
Q

What does event-driven programming mean?

A

uses events to trigger various functions - eg. a key or clicking a mouse button

19
Q

What is an Event Loop in Node.js?

A

The Event Loop handles async callbacks and is the foundation of the non-blocking input/output in Node

20
Q

Differentiate between process.nextTick() and setImmediate()

A

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

21
Q

What is an EventEmitter in Node.js?

A

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

22
Q

What are the two types of API functions in Node.js?

A

async, non-blocking functions

sync, blocking functions

23
Q

What is the package.json file?

A

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

24
Q

How would you use a URL module in Node.js?

A

Provides various utilities for url resolution and parsing to help split up web addresses into a readable format

25
Q

How do you create a simple Express.js application?

A

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

26
Q

What are streams in Node.js

A

Objects that enable you to read or write data continuously (types of streams: readable, writeable, duplex, and transform)

27
Q

How do you install, update, and delete a dependency?

A

npm install
npm update
npm uninstall

28
Q

How do you create a simple server in Node.js that return Hello World?

A

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

29
Q

Explain asynchronous and non-blocking APIs in Node.js

A

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

30
Q

What is REPL in Node.js

A

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

31
Q

What is the control flow function?

A

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