NodeJs Flashcards

1
Q

CommonJS modules ( 3 characteristics)

A

Uses require and module.exports
Synchronous loading
Default in nodeJs

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

ECMAScript Modules ( 3 characteristics)

A

Uses import and exports , standard JS
Asynchronous loading
Works in nodejs and browsers

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

How to use ECMAScript modules

A

package.json = { “type”: “module” }

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

Code to create a http server

A

import http from ‘http’
http.createServer( (req, res) => {})
server.listen( port, err => {})
res.writeHead( code, { “header”:”content”})
res.write(data);
res.end()

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

fs (File System)

A

Handles file operations such as reading, writing and deleting files.
writeFileSync(name, text)
readFileSync( path,encoding)

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

path

A

Helps with working with file and directory paths
path.join(_dirname, filename)

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

https

A

Similar to http but handles htpps secure requests.
https.get( url, (res) => res.on())

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

os

A

Provides OS related utilities
os.platform()
os.totalmem()
os.freemem()

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

crypto

A

Provide cryptographic functions as hasing and encryption.
const hash = crypto.createHash(‘sha256’).update(‘password123’).digest(‘hex’);

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

events

A

Implements an event-driven architecture using EventEmitter.
new EventEmitter();
emitter.on(‘event’, callback)
emitter.emit(‘event’)

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

util

A

Provides utilities such as promistificattion of callback-based functions.

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

child_process

A

Execute shell commands and external programs.
exec(‘ls’, (error,stdout, stderr) => {})

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

stream

A

Handles streaming data, large files.
const readStream = fs.createReadStream(‘example.txt’, ‘utf-8’);
readStream.on(‘data’, chunk => console.log(chunk));

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

3 Javascript Execution data

A

JS is synchronous
Js is blocking
Js is single-threated

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

What is NodeJs Event Loop?

A

C program that coordinates the execution of code in Node.Js uses V8 engine and libuv library for sync and sync ops.

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

How does V8 and libuv work?

A

V8 has the memory memory head and the call stack for sync ops, libuv works only when we have async functions

17
Q

What is libuv?

A

Libuv is a cross-platform open-source library written in C. In the Node.js runtime, its role is to provide support for handling asynchronous operations.

18
Q

Does Node wait for the call stack to be empty before running the callback function, or does it interrupt the normal flow of execution to run the callback function?

A

Node.js does NOT interrupt the normal flow of execution to run an asynchronous callback, it waits for the call stack to be empty.

19
Q

Is valid to use await at js top level file?

A

await at the top level is only valid in ES modules.

20
Q

What are the phases of the Event Loop?

A
  1. Timers
  2. I/O Poll
  3. Check Queue
  4. Close Queue
21
Q

What are microstasks in the Event Loop?

A

This tasks are process.NexThick or promises.resolve that get executed right away, without waiting for the next phase.

22
Q

Does Node wait for the call stack to be empty before running the callback function, or does it interrupt the normal flow of execution to run the callback function?

A

Node.js does NOT interrupt the normal flow of execution to run an asynchronous callback, it waits for the call stack to be empty

23
Q

If two async tasks such as setTimeout and readFile complete at the same time, how does Node decide which callback function to run first on the call stack? Does one get priority over the other?

A

Timer callbacks are executed before I/O callbacks, even if both are ready at the exact same time.

24
Q

What are I/O ops?

A

Input Output ops.
Http Requests, Reading from a File, sockets