How should developers properly handle unhandled promise rejections in Node.js?

  • Using a global event listener for 'unhandledRejection' events.
  • Ignoring unhandled promise rejections.
  • Manually catching every promise rejection.
  • Using try...catch for all promises.
Properly handling unhandled promise rejections in Node.js involves using a global event listener for 'unhandledRejection' events. This allows developers to log or handle unhandled promise rejections globally, which is crucial for preventing silent errors in applications. Ignoring rejections or manually catching every promise rejection can lead to unexpected and unhandled errors. Using try...catch for all promises is not practical in large applications.

You are designing a RESTful API and want to ensure that it is secure against injection attacks, what are the various considerations and practices you would implement to sanitize and validate input data?

  • Use parameterized queries and prepared statements.
  • Implement input validation and sanitize user inputs.
  • Avoid using HTTPS for data transmission.
  • Rely on client-side validation for security.
Option (1) is correct. Parameterized queries and prepared statements help prevent SQL injection attacks. Option (2) is also correct, as input validation and sanitation are important for protecting against various injection attacks. Options (3) and (4) are incorrect and insecure practices.

What is the significance of the main field in the package.json file when publishing a package?

  • It specifies the name of the package.
  • It indicates the primary entry point of the package.
  • It defines the package's version.
  • It lists all the dependencies of the package.
The main field in the package.json file is significant because it specifies the primary entry point of the package. This entry point is the file that will be loaded when someone requires the package. The other options are not the main purpose of the main field.

You are developing a system where precision is critical, and you have to handle very large integers. Which data type would you use to ensure there is no loss of precision?

  • int
  • float
  • bigint
  • double
When handling very large integers with critical precision, you should use the bigint data type. Unlike int and float, bigint can represent arbitrarily large integers without loss of precision. double is a floating-point type, which may not preserve precision for very large integers.

Which property of the response object is used to send an HTTP status code to the client?

  • statusCode
  • status
  • httpStatus
  • responseCode
To send an HTTP status code to the client in Node.js, you use the statusCode property of the response object. For example, response.statusCode = 200; sets the status code to 200 (OK). The other options are not standard properties for setting the status code.

You need to implement a feature where documents from a MongoDB collection are automatically archived after a certain period. How would you approach this requirement using Mongoose functionalities?

  • Use a cron job to periodically scan and archive documents.
  • Modify the MongoDB collection to automatically archive documents on insertion.
  • Manually move documents to an archive collection after a certain period.
  • Implement a trigger to archive documents on update.
To automatically archive documents in MongoDB using Mongoose, you can use a cron job (Option 1). This allows for periodic scanning and archiving based on your criteria. Options 2, 3, and 4 are not typical approaches for automatic archiving and may not be as efficient or maintainable.

How can you implement a custom event emitter using the Events module in Node.js?

  • By directly extending the 'EventEmitter' class.
  • By using a third-party library like 'emitter.js'.
  • By creating a regular JavaScript function with event handling capabilities.
  • By using promises and async/await.
To implement a custom event emitter in Node.js, you can create a new class that extends the built-in 'EventEmitter' class. This allows you to inherit all the event-related functionality and add custom events and event handling methods.

How can you ensure a specific listener is called only once for an event in Node.js?

  • event.once(event, listener)
  • event.addOnceListener(event, listener)
  • listener.once(event)
  • event.addSingleListener(event, listener)
To ensure that a specific listener is called only once for an event in Node.js, you can use the event.once(event, listener) method. This method registers the listener to be invoked only once the next time the specified event is emitted. The other options do not provide this functionality.

Mock objects are primarily used to ______ the interactions between the object under test and its dependencies.

  • Record
  • Ignore
  • Validate
  • Isolate
Mock objects are primarily used to validate or verify the interactions between the object under test and its dependencies. They record method calls and parameters and allow you to assert that certain interactions occurred during the test.

You are publishing a package to npm, and you want to make sure that all tests pass before the package is actually published. Which npm script would you use to run your tests before the package gets published?

  • test
  • prepublishOnly
  • pretest
  • validate
To run tests before publishing a package to npm, you should use the prepublishOnly script. This script runs before both npm publish and npm publish --dry-run, making it suitable for pre-publish checks. The other options are not specifically designed for this purpose.