NodeJS Flashcards

(21 cards)

1
Q

NPM: Install specific version (1.2.0)

A

npm install cowsay@1.2.0

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

NPM: Remove specific package

A

npm uninstall cowsay

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

NPM: Update specific package

A

npm update cowsay

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

NPM: All packages for update

A

npm outdated

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

NPM: Show all versions of a package

A

npm view cowsay versions

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
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
7
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
8
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
9
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
10
Q

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

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
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
12
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
13
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
14
Q

Write and read from a file

A
await fs.writeFile("message.txt", "content"); // Write entire at once
await fs.appendFile("message.txt", " I love Node.js"); // append
await fs.readFile("message.txt", "utf8"); // Reads entire file at once
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
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
16
Q

Get hostname

A

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

17
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

18
Q

What is nodeJS Event Emitter

A

NoteJS events that can be listened to or emitted - sort of built-in observer.

```javascript
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
~~~

19
Q

Create empty Buffer of 1024 bytes.

A

const buf = Buffer.alloc(1024);

20
Q

Create Buffer from string Hey

A

const buf = Buffer.from(“Hey!”);

21
Q

Access first byte of a Buffer