Node Flashcards

1
Q

process

A

Node’s global object containing information about the current process and some methods.

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

pocesss.env

A

Information about the environment in which the process is currently running.

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

process.argv

A

An array of command line values provided when the current process was initiated.

process. argv[0] - the absolute path to Node which ran the process
process. argv[1] - the path to the file that’s running
process. argv[2 -> …] - command line arguments provided when the process was initiated.

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

req, res

A

Objects with properties and methods that we can use to customise our HTTP requests.

req

  • the object which contains information about the client HTTP request
  • an instance of IncomingMessage

res

  • provides methods for sending back the server HTTP response
  • an instance of ServerResponse, which is a writeable stream
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Main response time cost

A

Usually the SUM of time it takes to execute ALL db queries.

In Node, you can execute all the db queries IN PARALLEL, reducing the response time to that of the slowest db query.

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

What is Node?

A
  1. an synchronous event driven runtime, or environment, that allows for execution of JS outside of the browser
  2. a library (comes with a lot of moudles)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

package.json

A

Project’s control file

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

Are expensive operations OK in Node?

A

Yes, al long as you make sure not to block the Node.js process with them.

You could spawn a child process (child_process.fork()) and take advantage of multiple cores in your environment.

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

How can you use multiple threads in Node?

A

You could spawn a child process (child process.fork()) and take advantage of multiple cores in your environment, even though Node was designed without threads.

If you need more instances of the event loop, you can create while them making sure that global data in each instance isn’t shared. If your instances need to collaborate, your need some communication mechanism.

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

Event loop as a runtime construct

A

Node presents an event loop as a RUNTIME CONSTRUCT instead of as a LIBRARY.

There is no start-the-event loop call:

  • Node simply enters it after executing the input script, and
  • Exits it when there are no tasks callbacks left to perform

This behaviour is like the browser JS where the event loop is hidden from the user.

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

Blocking vs. Non-blocking calls

A

BLOCKING

  • execute synchronously
  • extremely cheap synchronous operations (e.g., console.log) don’t cause problems, but they can still stall the Node’s event loop, given that there are enough of them within one loop iteration.

NON-BLOCKING
- execute asynchronously

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

The most important rule for writing Node apps

A

Don’t block the event loop:

  • handle I/O intensive operations asynchronously
  • keep your code (everything that happens synchronously within one loop iteration) lean
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

How does Node work?

A

Node

  1. Starts
  2. Loads our app (into the event loop, it is already there)
  3. Loops until there’s nothing left to do
  4. Then our app terminates

In the case of an HTTP server, we create a background process that CONTINUOUSLY WAITS for requests from clients, so the app doesn’t terminate when there are no callbacks left to process.

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

What is performed within one loop iteration?

A

All the synchronous operations given. Async operations are left for future loop iterations.

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

Why are slow blocking operations a global problem in Node.

A

Because there is only one process, running on a single CPU core.

A slow DB query in Python would result in a slow page load for one particular user, while in Node that would be a global problem affecting all users.

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

Parallelism in Node

A

Everything runs in parallel, except your code.

Your code is the king, while Node is its army of servants which perform their tasks in parallel, but only report to king one at a time.

There can be only one callback firing at the same time. All the other callbacks have to wait for the first one to finish executing.

One benefit of such single-threaded/event loop design is that it eliminates the worry that different pieces of code will access the same data structure at the same time.

17
Q

What is the execution/parallelism model of Node?

A

The single-threaded event loop.

18
Q

What does Node do when you call an async function?

A

It starts an operation outside the event loop.

Conceptually, Node starts an async operation and makes a mental note that when this operations triggers an event, the callback that was passed to the operation needs to be called.

19
Q

How does a Node server work

A
  1. Node runs a single-threaded event loop registered with the system to handle connections.
  2. Each new connection causes a system callback to fire
  3. The callbacks handle requests with non-blocking I/O calls
    - If necessary, callbacks spawn threads from a pool to execute blocking or CPU intensive operations, and to load-balance across CPU cores
20
Q

Benefits of the Node’s single-threaded event loop execution model

A
  1. Node’s approach to scaling with callbacks requires less memory to handle more connections than most architectures that scale with threads.
  2. The single-threaded/event loop design eliminates the worry that different pieces of code will access the same data structure at the same time.
21
Q

fs

A

Provides simple wrappers around standard POSIX file I/O functions.

22
Q

The Event Emitter Pattern

A

Useful in situations when more than one callback event must be handled.

It’s a function that returns an Evt Emitter object which is a writable stream.

Using this object’s .on method we can attach callbacks to different events.

23
Q

The Event Emitter object

A

A writable stream that can be used to attach callbacks to different types of events.

24
Q

EvtEmitter.on(‘error’, …)

A

Will raise an exception if not handled by a callback.

25
Q

EvtEmitter.on(‘end’, …)

A

Fired once, and from then onward no other events will be fired from the evt emitter.

26
Q

EvtEmitter.once(‘evtName’, cbName)

A

Like the ‘.on’ method, but its event callbacks fire only once, on the first occurrence of the event of the type, and then are removed from the list of event listeners.

27
Q

Detach callbacks from EE’s listeners

A

ee. removeListener(‘evtName’, cbName)

ee. removeAllEventListeners(‘evtName’)

28
Q

When should you execute async operations in parallel in Node

A

When they don’t inherently depend on each other (one does not itself need the result of another operation), even when you need the results of both operations.

If you wait for each operation to finish before proceeding with the next one, main response time cost would be the sum of all times it took to perform each given operation. If you execute them in parallel, the main response time cost is that of the slowest of the operation.

29
Q

How to perform async operations in parallel

A

async.parallel

30
Q

How to perform async operations in series

A

async.series

31
Q

How to perform async operations in series, so that the subsequent operations get results of the ones preceding them

A

async.waterfall

32
Q

Core Node.js modules

A
fs
http
path
url
querystring
util