Why would you use the global object to store data in a Node.js application?
- To share data between different Node.js modules.
- To encapsulate data and prevent it from being accessed globally.
- To improve application performance by reducing memory usage.
- To ensure data persistence across application runs.
The global object in Node.js can be used to store data that needs to be shared between different Node.js modules. It acts as a global namespace for variables and allows you to share data across different parts of your application. The other options do not accurately describe the common use case for the global object.
When are CORS preflight requests sent by the browser?
- Before making certain types of requests
- After a successful request
- Randomly to check server compatibility
- Only when cookies are involved
CORS preflight requests are sent by the browser before making certain types of requests, specifically those that could have an impact on server security or state. These preflight requests are used to check with the server if the actual request (e.g., a cross-origin POST request with custom headers) is allowed, ensuring server compatibility and security.
You are working on a Node.js project with a team, and you notice that the package-lock.json file is frequently causing merge conflicts. How would you resolve or minimize such conflicts while ensuring consistent dependency resolution?
- Remove the package-lock.json file entirely
- Use Yarn instead of npm
- Use a tool like npm ci and enforce it in your team's workflow
- Manually edit the package-lock.json file in case of conflicts
To minimize package-lock.json merge conflicts, you should use a tool like npm ci in your team's workflow. npm ci installs dependencies based on the package-lock.json and ensures consistent dependency resolution. Options 1 and 2 are not recommended, and option 4 is not a practical solution.
What is the difference between chaining multiple .then() methods and using multiple await expressions?
- Chaining .then() is more efficient for error handling
- Chaining .then() is better for readability
- Using multiple await expressions allows better error propagation
- Using multiple await expressions guarantees faster execution
When using await expressions, errors can be propagated using standard try/catch blocks, which allows for more granular and flexible error handling. Chaining multiple .then() methods can lead to less readable and maintainable code when dealing with multiple asynchronous operations. It's not a question of efficiency or speed but rather about readability and error handling.
The prototype of an instance object in JavaScript is found using the ______ property.
- __proto__
- instanceOf
- type
- parent
The __proto__ property is used to access the prototype of an instance object in JavaScript. This is how objects inherit properties and methods from their constructor's prototype.
Express.js middleware functions have access to the ______ object, the ______ object, and a next function in their callback function parameters.
- request, response
- response, request
- req, res
- req, next
In Express.js middleware functions, the callback function parameters typically include req (the request object) and res (the response object). Additionally, you can use a next function to pass control to the next middleware function in the pipeline. This allows you to manipulate the request and response objects or perform actions before sending a response to the client.
You are maintaining a server that has strict security requirements. You need to allow cross-origin requests but with stringent restrictions. How can you implement CORS to fulfill these requirements while maintaining security?
- Set Access-Control-Allow-Origin to * and rely on server-side authentication.
- Implement preflight requests with custom headers and allow only authorized clients.
- Avoid using CORS and handle cross-origin requests through server-side scripting.
- Enable CORS for all origins and use server-side IP filtering.
To implement stringent security with CORS, you should use preflight requests with custom headers to allow only authorized clients. Option A is not secure as it allows any origin. Option C suggests avoiding CORS altogether, which may not be practical. Option D relies on IP filtering, which can be bypassed.
To install all the dependencies listed in the package.json file, the ______ command should be used.
- npm init
- npm add
- npm install
- npm create
To install all the dependencies listed in the package.json file, you should use the npm install command. It reads the dependencies specified in package.json and installs them. The other options are not used for this specific purpose.
In Sequelize, which method is commonly used to find a single instance from the database?
- findAll()
- findOne()
- findSingle()
- fetchOne()
In Sequelize, the commonly used method to find a single instance from the database is findOne(). This method retrieves the first matching record that meets the specified criteria. The other options, findAll(), findSingle(), and fetchOne(), do not represent the standard method for retrieving a single instance in Sequelize.
In JavaScript, a for…in loop is used to iterate over the ________ of an object.
- properties
- values
- methods
- variables
In JavaScript, a for...in loop is used to iterate over the properties of an object. It is commonly used for object iteration, and it iterates through the keys or property names of an object.