Node.js Flashcards

1
Q

What is Node.js?

A

Node.js is a runtime environment that allows you to run JavaScript on the server-side. It is built on Chrome’s V8 JavaScript engine and uses an event-driven, non-blocking I/O model, making it lightweight and efficient.

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

How does Node.js handle asynchronous I/O?

A

Node.js handles asynchronous I/O using the event loop. It allows the execution of non-blocking operations, and when an operation completes, a callback function is called. Promises and async/await are also used to manage asynchronous code more efficiently.

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

What are the advantages of using Node.js?

A
  • Fast execution due to the V8 engine.
  • Non-blocking I/O operations.
  • Single-threaded event-driven model.
  • Large ecosystem of packages with npm.
  • Same language for both client and server-side scripting
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is the event loop in Node.js?

A

The event loop is a fundamental part of Node.js’s architecture. It allows Node.js to perform non-blocking I/O operations by offloading operations to the system kernel whenever possible. The event loop continuously checks the call stack to see if there’s any function that needs to run.

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

What is Buffer in Node.js?

A

Buffer is a temporary memory, mainly used by the stream to hold some data until consumed. Buffer is mainly used to store binary data while reading from a file or receiving packets over the network.

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

What is Stream in Node.js?

A

Stream is the object (abstract interface) that allows us to transfer data from source to destination and vice-versa. It enables you to process large amounts of data chunk by chunk, without having to load the entire data set into memory at once.

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

What are types of Stream in Node.js?

A
  • Readable Stream
  • Writable Stream
  • Duplex Stream: A Duplex stream is both readable and writable
  • Transform Stream: A Transform stream is a Duplex stream that performs transformations on the data as it is read and written. e.g. Gzip library
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is promise in Node.js?

A

A promise in JavaScript is an object representing the eventual completion (or failure) of an asynchronous operation and its resulting value. Promises provide a cleaner and more flexible way to handle asynchronous operations compared to traditional callback-based approaches.

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

What is cluster in Node.js?

A

The cluster module allows you to create child processes (workers) that share the same server port. This way, you can distribute the load across multiple cores of the CPU, improving the performance and reliability of your application. The primary purpose of the Cluster module is to distribute incoming connection requests (e.g., HTTP requests) across a pool of workers, allowing a Node.js server to handle multiple requests concurrently.

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

What is the Node.js process model?

A

The Node.js process model revolves around the event-driven, single-threaded, non-blocking architecture, which optimizes the handling of concurrent operations and I/O-bound activities, making it well-suited for building scalable, high-performance applications.

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

What is difference between readFile and createReadStream in Node.js

A

The readFile method is used to asynchronously read the entire contents of a file into memory as a single buffer.
The createReadStream method is used to create a readable stream from a file.

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

What are the security best practices for Node.js applications?

A
  • Dependency Management. Regularly update and patch dependencies to address security vulnerabilities.
  • Input Validation and Sanitization. Validate and sanitize user input to prevent injection attacks, such as SQL injection, NoSQL injection, and cross-site scripting (XSS).
  • Secure Authentication and Authorization. Implement secure authentication mechanisms, such as bcrypt for password hashing, and adopt industry-standard protocols like OAuth or JWT for authorization
  • Secure Communication. Utilize HTTPS with TLS/SSL to secure communication between clients and the server.
  • Avoiding Sensitive Data Exposure. Utilizing environment variables and secure storage mechanisms rather than embedding them directly within the codebase.
  • Security Headers. Set appropriate security headers.
  • Logging and Monitoring
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q
A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly