Section 1

Preview this deck

How to avoid callback hell?

Front

Star 0%
Star 0%
Star 0%
Star 0%
Star 0%

0.0

0 reviews

5
0
4
0
3
0
2
0
1
0

Active users

0

All-time users

0

Favorites

0

Last updated

6 years ago

Date created

Mar 1, 2020

Cards (53)

Section 1

(50 cards)

How to avoid callback hell?

Front

* modularization -> break callbacks into independent functions * use async/await * use generators with Promises

Back

Which is the first argument usually passed to a Node.js callback handler?

Front

error object (may be null if no errors occur)

Back

What is event emitter?

Front

EventEmitter lies in events module let events = require('events'); let eventEmitter = new events.EventEmitter(); EventEmitter provides properties like on and emit - on is used to bind a function with an event - emit is used to fire an event

Back

What are streams in Node.js?

Front

Streams are objects that allow reading of data from the source and writing of data to the destination as a continuous process

Back

Explain the event loop?

Front

See https://developer.mozilla.org/en-US/docs/Web/JavaScript/EventLoop * TL;DR allows javascript to perform non-blocking I/O operations

Back

What keyword do you need to use to insert a debug breakpoint?

Front

debugger keyword

Back

What tools can be used to assure consistent style? Why is this important?

Front

ESLint... it is important so team members can modify more projects easily and understand how code is written without having to relearn different styles

Back

When are background/worker processes useful?

Front

These are useful when you want to do data processing in the background such as sending out emails or processing images

Back

Name the types of API functions in Node.js

Front

* Blocking functions -> all other code is blocked from executing until I/O event that is being waited on occurs *Non-Blocking Functions: Multiple I/O calls can be performed without the execution of the program being halted

Back

What is a prototype chain?

Front

object -> reference to object -> reference to object person1 instance prototype inherits from Person prototype which inherits from Object prototype

Back

How to read a file using Node?

Front

fs.read(fd, buffer, offset, length, position, callback) fd -> file descriptor returned by fs.open() method buffer -> this is the buffer the data will be written to offset -> offset in buffer to start writing at length -> integer specifying number of bytes to read position -> an integer specifying where to begin reading from the file callback -> call back function that gets params (err, bytesRead, buffer)

Back

Object literal vs Object instantiation?

Front

1. Object literal let obj1 = {} has no constructors 2. Object instantiation using new keyword to create objects and have constructor 3. object literal variable acts as one instance (singleton) ... setting variables equal to them refer to the same instance! 4. Object instantiation approach allows you to make different objects of the same type

Back

What are exit codes in Node.js?

Front

Exit codes are specific codes used to end a "process 1. Unused 2. Uncaught Fatal Exception 3. Fatal Error 4. Non-function Internal Exception Handler 5. Internal Exception handler Run-Time Failure 6. Internal JavaScript Evaluation Failure

Back

What is Buffer class in Node?

Front

Buffer class is a global class that can be accessed in the app without importing buffer module. A buffer is kind of array of ints and corresponds to raw memory allocation outside the V8 heap

Back

What is __dirname?

Front

A global variable that returns the absolute path of directory containing the current file

Back

What is Node.js?

Front

Node.js is a web app framework built on Google Chrome's JavaScript V8 engine. Runtime allows to execute js code on any machine outside a web browser.

Back

What are 2 methods in the fs module that can be used for reading a whole file at once?

Front

fs.readFile and fs.readFileSync

Back

What is a blocking function

Front

Must be completed before other statements are executed

Back

Explain chaining in Node.js?

Front

Chaining is a mechanism whereby the output of 1 stream is connected to another stream creating a chain of multiple stream operations

Back

What is Node.js?

Front

Is a server side scripting based on Google's V8 JavaScript engine. Used to build web apps

Back

Name Truthy Values in JS

Front

1. true keyword 2. empty object {} 3. empty array [] 4. non-zero number 42 or -42 5. new Date() 6. 12n bigint 7. 3.14 or -3.14 floats 8. Infinity or -Infinity

Back

Name some flags used to read/write operation on files

Front

r -Open file for reading. exception occurs if file does not exist r+ -> open for file read and write rs -> open file for reading in sync mode w -> open file for writing. creates file if it doesn't exist or overwrites file if it exists a -> open file for appending

Back

What is a callback?

Front

Callback is an asynchronous equivalent for a function. Callbacks are called when a task is completed.

Back

How are objects that generate events called and which class from the events module are they instances of?

Front

Objects are called event emitters and are instances of EventEmitter class.

Back

What are Globals in Node.js?

Front

* Global - represents Global namespace object and acts as a container for all other <global> objects * Process - one of the global objects but can turn a synchronous function into an async callback. It can be accessed anywhere in the code and it primarily gives back info about app or environment * Buffer: it is a class in Node.js to handle binary data

Back

What is a callback function?

Front

A function written in a client script that runs asynchronously. Can finish at anytime depending if there is network call to be made internally

Back

What are async and await?

Front

The await expression pauses execution of function and waits for resolution of passed promise. Then function's execution resumes and returns resolved value. (Makes code run synchronously easier to think about)

Back

Difference between Node.js and Ajax?

Front

Ajax (Async Javascript and XML) is designed for dynamically updating a particular section of a page's content without having to update the entire page. Node.js used for developing client-server applications

Back

What is Piping in Node?

Front

Piping is a mechanism to connect output of onstream to another stream. Used to get data from one stream and output to another stream.

Back

Which global variable can be used to access information about the app and the environment that it runs in?

Front

The process variable

Back

What is LTS releases of Node.js why should I care?

Front

LTS = (Long Term Support) version of Node.js receive critical bug fixes, security updates and performance udpates * LTS versions are supported for at least 18 months * Best for production since LTS release line is focused on stability and security whereas Current release line has a shorter lifespan and more frequent updates to the code

Back

Name Falsy values in JS

Front

1. false keyword 2. number 0 3. BigInt 0n 4. "", '', or ``, empty strings 5. null 6. undefined 7. NaN

Back

T/F Node.js is multi-threaded

Front

False

Back

What is Node.js Cluster module?

Front

module used to take advantage of multi-core systems, so apps can handle more load

Back

What is REPL?

Front

Read Evaluate Print Loop

Back

Which keywords are used to export and import an object or a function from a module file?

Front

module.exports to export an object or function require to import an object or function

Back

What's a stub?

Front

Stubs are functions/programs that simulate behaviors of components/modules. Stubs provide hypothetical answers to function calls made during test cases.

Back

What's the difference between setTimeout and setImmediate?

Front

setTimeout is used to run a function after a certain minimal timeout. setImmediate is used to run a function once the current event loop completes.

Back

Why is JavaScript described as a prototype-based language?

Front

Objects in JavaScript are considered template objects where they inherit methods and properties from.

Back

What is callback hell?

Front

Results in heavily nested callbacks that make code not only unreadable but difficult to maintain

Back

Name the 4 types of streams

Front

* <Readable> to facilitate the reading operation * <Writable> to facilitate the writing operation * <Duplex> to facilitate both read and write operations * <Transform> is a form of Duplex stream that performs computations based on the available input

Back

What is the preferred method of resolving unhandled exceptions in node.js

Front

exceptions can be caught at the process level. process.on('uncaughtException', (err) => { console.log(`caught exception: ${err}`); });

Back

What are the functionalities of NPM in Node.js?

Front

Node Package Manager: 1. Online repo for Node.js packages 2. Command Line utility for installing packages, version management, and dependency management of Node.js packages

Back

What is libuv?

Front

C library that implements the event loop and all of async behaviors

Back

What does "callback hell" mean?

Front

Plenty of nested callbacks making code difficult to read and maintain

Back

How can you secure your HTTP cookies against XSS attacks?

Front

* XSS (Cross Site Scripting) occurs when the attacker injects executable JS code into HTML response. * To mitigate the attack, must set flags on the set-cookie HTTP header: HttpOnly - attribute used to help prevent attacks such as xss since it does not allow cookie to be accessed via Javascript

Back

What are promises?

Front

* A Promise can either be resolved or rejected * Useful for making async operations * const promise1 = new Promise(function(resolve, reject) { setTimeout(function() { resolve('foo'); }, 300); });

Back

T/F Prototype chains copy methods and functions from one object to another

Front

False. They are accessed by walking up the chain if child prototype does not contain a function definition for it

Back

How many types of streams are present in Node?

Front

4 types of streams are present. 1. Readable - Stream which is used for read operation 2. Writable - Stream which is used for write operation 3. Duplex - stream for both read and write operation 4. Transform -> type of duplex stream where output is computed based on input

Back

How to open a file using Node?

Front

fs.open(path, flags[, mode], callback) path -. string containing path to file flags -> tells behavior of file to be opened ('w' as example) mode -> sets the file mode (permission and sticky bits) callback -> function which gets 2 arguments (err, fd)

Back

Section 2

(3 cards)

How to write to a file using Node?

Front

fs.writeFile(filename, data[, options], callback) params path -> string having file name including path data -> string or buffer to be written to file options callback -> this is the callback function whic hgets a single parameter err and used to return error in case of any writing error

Back

What is a stub?

Front

Stubs are functions/programs that simulate behaviours of components/modules. Stubs provide canned answers to function calls made during test cases

Back

What is currying?

Front

A process in functional programming in which we can transform a function with multiple arguments into a sequence of nesting functions Example: function multiply(a) {return (b) => {return (c) => {return a b c}}}

Back