NodeJS Flashcards

(22 cards)

1
Q

How to access environment variables

A

process.env.USER_ID;

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

Print stack trace to a console

A

console.trace()

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

Start and stop timer, print time to the console

A
console.time("doSomething()");
console.timeEnd("doSomething()");
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Create path object from a string

A

const parsed = path.parse("/users/john/project/file.txt")

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

Get filename from "/etc/test/file.txt (file.txt)

A

path.basename("/etc/test/file.txt")

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

Create path string from components [\_\_dirname, "test", "hello.html"]

A

path.join(\_\_dirname, "test", "hello.html")

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

Remove obsolete ../ from "/users/joe/..//test.txt"

A

path.normalize("/users/joe/..//test.txt");

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

How to use async fs methods

A

import fs from "node:fs/promises";

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

Write or read entire file having fh handle ready

A
await fh.writeFile("Overwrite content");

const content = await fh.readFile({ encoding: "utf8" });
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Get user home directory

A

import os from "node:os";
os.homedir();

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

Create URL object from a string, what you need to import

A

const myUrl = new URL( "http://mywebsite.com:5000/hello.html?id=100&status=active" )

Nothing to import, it is global constructor

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

What is nodeJS Event Emitter, how to use it (what to import, sample code)

A

Built-in observer pattern implementation,

import EventEmitter from "node:events";

const eventEmitter = new EventEmitter();

eventEmitter.on("start", (start, end) => console.log("started"));
eventEmitter.emit("start", 1, 100); // Can use any string for event identification
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Create empty Buffer of 1024 bytes.

A

const buf = Buffer.alloc(1024);

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

Create Buffer from string Hey

A

const buf = Buffer.from("Hey!");

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

Access first byte of a Buffer

A

buf[0];

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

How to force NodeJS production mode

A

NODE_ENV=production

17
Q

Read file handle fh line by line and print each line.

A

fh.readLines() returns an async iterable so we need to use for await loop!

for await (const line of fh.readLines()) {
  console.log(line);
}
18
Q

Open file (get fh) in write mode

A

const fh = await fs.open("temp.txt", "w");

19
Q

Convert Buffer buf to UTF-8 string

A

buf.toString();

20
Q

What are Streams in NodeJS, give sample streams in NodeJS API

A
  • All streams are instances of EventEmitter, can use events to read and write data.
  • Streams process data in chunks, significantly reducing memory usage.

Sample streams:
- fs.ReadStream
- http.IncomingMessage when reading HTTP requests.
- process.stdin
- fs.WriteStream
- process.stdout
- process.stderr

21
Q

How to create a pipeline with streams (like linux readableSrc | t1 | t2 | final). Show both methods

A

pipe - legacy

readableSrc.pipe(t1).pipe(t2).pipe(final);

pipeline This method is a safer and more robust way to pipe streams together, handling errors and cleanup automatically.

pipeline(readStream, upper, writeStream, (err) => {
  if (err) {
    return console.error("Pipeline error:", err.message);
  }
  console.log("Pipeline succeeded");
});
// In case of error, all streams will be closed.
22
Q

What is backpressure in nodejs streams

A

Backpressure (Buffering)

  • When using streams, it is important to make sure the producer doesn’t overwhelm the consumer.
  • For this, the backpressure mechanism is used in all streams in the Node.js API, and implementors are responsible for maintaining that behavior.
  • In any scenario where the data buffer has exceeded the highWaterMark or the write queue is currently busy, .write() will return false.
  • When a false value is returned, the backpressure system kicks in. It will pause the incoming Readable stream from sending any data and wait until the consumer is ready again.
  • Once the data buffer is emptied, a 'drain' event will be emitted to resume the incoming data flow.