Node.js Flashcards
What is Node.js?
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 does Node.js handle asynchronous I/O?
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.
What are the advantages of using Node.js?
- 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
What is the event loop in Node.js?
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.
What is Buffer in Node.js?
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.
What is Stream in Node.js?
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.
What are types of Stream in Node.js?
- 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
What is promise in Node.js?
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.
What is cluster in Node.js?
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.
What is the Node.js process model?
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.
What is difference between readFile and createReadStream in Node.js
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.
What are the security best practices for Node.js applications?
- 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