Node JS Flashcards

1
Q

What is Node.JS ?

A

Node.js uses an event-driven, non-blocking I/O model .

It is a runtime environment which means its self-encapsulated

It is single threaded with an Event Loop

It is just a terminal for background scripting (pure logic, no UI, no CSS, no DOM )

Javascript needs to work with C++ so we can write JS code to control C++ built features that allow control of our computer’s internal features.

JS -> Node -> Computer Feature (e.g. network, file system)

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

What is LibUV ?

A

A library that helps Node.js to do I/O operations with OS threads to avoid blocking the main thread

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

What is the Event Loop?

A

Event Loop determines what function/code to run next from the queues by repeatedly checking each queue.

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

What are the Six Queues/Phases and what builds it?

A

All the queues are built by Node C++

Timers

callback Queue

Idle

I/O Callback

Microtask

Check Queue

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

What 3 Languages are used to load a web app ?

A

HTML - things/elements

CSS - decorate/ prettify

Javascript - logic and interaction

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

Where does Data and Code come from?

A

Server ( basically another computer) that is connected to the internet always on and ready to receive a message

These messages specifically request data and code and the server decides what do I want to send back

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

What is basic explanation of how Node.JS works ?

A

User computer sends inbound message to computer’s internal features.

To access it, we need to use Node C++ features

We will write Javascript code to affect Node C++ features which then connect us to Computer’s internal features

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

What is the 3 things that Javascript does in this situation?

A
  1. Saves Data and Functionality (Code)
  2. Uses that data by running functionality (code) on it
  3. Js has a ton of built-in label that trigger Node features that are built in C++ to use our computer’s internals
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What does HTTP mean and what does it feature do in NODE?

A

Hypertext Transfer Protocol

we use HTTP feature of Node to set up an open socket

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

What is socket ?

A

open channel for data to go in and out of a place over the Internet

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

How do we Call Methods in Node?

A

Node auto-runs the code (function) for us when a request arrives from a user

we have to rely on Node to trigger Js code to run

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

What are the steps to creating a server behind the scenes?

A

User Computer sends inbound message to Computer’s Internal Features and LibUV connects to NODE

Node will set up feature to access computer background internal features (e.g. open a socket) and Auto Run the doOnIncoming code and throw into Javascript

The Inbound message will trigger Node to create two auto created Javascript objects

          1. 1st object will be able to receive incoming relevant data like taking URL from inbound message
          2. 2nd object will have many functions that allow access  to Node (e.g End, Write func) . It is labeled functionsToSetOutGoingData 

Javascript will be triggered to run what Node gave it with the 2 objects passed in arguments in JS function and it will return data to the 2nd object for the outbound message.

JS will create server object that has function Listen so we can edit background C++ features like edit port number

We will rely on libUV to grab the outbound message from Node and send to Computer’s Internal Features which is then sent back to User Computer in readable format

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

What does Require Function do?

A

This function allows us to access each of Node C++ features independently

const http = require( ‘ http’ )

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

What is Error Handling like in Node?

A

Node will automatically send out the appropriate Event emitter depending on what it gets from the computer internals (http message or error)

If happy nicely formatted
REQUEST then auto run a function
Server.on(‘request’, doOnIncoming )

If corrupted
clientError then auto run a specific function
Server.on(‘clientError’, doOnError)

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

What is JSON?

A

JSON is a javascript-ready data format
Its just a string of characters
Easy way to take object to workable javascript format

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

Describe fs.readFile

A

fs. readFile is the specific label built into JS that gives access to node
fs. readFile(‘./tweets.json’ , useImportedtweets )

to Javascript, ‘tweets.json’ is just a string of characters but Node C++ understands that this is the location in the computers memory

17
Q

What does dot and slash mean?

‘./tweets.json’

A

Dot means current folder in which you are running node

Slash says look in that folder

18
Q

What is the Streams feature of Node?

A

Streams are objects that let you read data from a source or write data to a destination in continuous fashion.

Node will clean while adding new things to the callback queue and never buffers any data.

Node will do things in batches and auto broadcast a message (e.g. 64 bytes per batch)

The applications output the data in chunks

First 64 bytes batch arrives and we have the first auto created and inserted data

Data event is going to be streamed out
This will cause auto run of doOnNewBatch in JS

Make new execution Context

Then the next 64 bytes come in and goes into the callback queue

19
Q

What are the 3 Rules for automatic execution of the JS Code by Node?

A
  1. Hold each deferred function in one of the task queues when the Node background API completes
  2. Add the function to the Call Stack ONLY when the call stack is totally empty
     Event Loops checks this condition

3.Prioritize functions in Timer queue over I/O queue, oversetImmediate (‘check’) queue

20
Q

What app do you use to execute Node.JS ?

A

CLI ( Command Line Interface) to execute your node apps

REPL (Read Evaluate Print Loop ) for playing around

21
Q

What are some Global Objects/Functions ?

A

Node.js global objects are global in nature and they are available in all modules.

Process - has information about the environment the program is running in

Console - Used to print information on stdout and stderr.

Require - function to find and use modules in the current module

_dirname
_filename

setTimeout(cb, ms) global function is used to run callback cb after at least ms milliseconds.

clearTimeout(t) global function is used to stop a timer that was previously created with setTimeout()

setInterval(cb, ms) global function is used to run callback cb repeatedly after at least ms milliseconds.

22
Q

What is a callback ?

A

Callback is an asynchronous equivalent for a function.

A callback function is called at the completion of a given task.

This makes Node.js highly scalable, as it can process a high number of requests without waiting for any function to return results.

23
Q

What does it mean when Node.Js is Asynchronous and Event Driven?

A

It essentially means a Node.js based server never waits for an API to return data.

The server moves to the next API after calling it and a notification mechanism of Events of Node.js helps the server to get a response from the previous API call.

24
Q

What is REPL?

A

REPL stands for Read Eval Print Loop

it represents a computer environment like a Windows console or Unix/Linux shell where a command is entered and the system responds with an output in an interactive mode.

Node comes bundled with a REPL environment.

25
Q

What is NPM?

A

Node Package Manager (NPM) provides two main functionalities −

Online repositories for node.js packages/modules which are searchable

Command line utility to install Node.js packages, do version management and dependency management of Node.js packages.

26
Q

What is EventEmitter?

A

Many objects in a Node emit events, for example, a net.Server emits an event each time a peer connects to it, an fs.readStream emits an event when the file is opened.

All objects which emit events are the instances of events.EventEmitter.