You are developing a RESTful API using the http module and need to support CORS. How would you implement CORS headers to handle pre-flight requests in your HTTP server?

  • Access-Control-Allow-Origin: *
  • Access-Control-Allow-Origin: mydomain.com
  • Access-Control-Allow-Origin: true
  • Access-Control-Allow-Origin: false
To support CORS in your HTTP server, you should set the Access-Control-Allow-Origin header to the specific domain (e.g., mydomain.com) from which you want to allow requests. Using a wildcard (*) is less secure as it allows any domain, and using true or false is not the correct syntax for this header.

How can you ensure that a specific version of npm is used in your Node.js project?

  • By defining the npm version in the package.json file.
  • By modifying the NODE_PATH environment variable.
  • By running the 'npm use' command.
  • By using a package manager like Yarn instead of npm.
You can ensure a specific version of npm is used in your Node.js project by defining the desired npm version in the engines field of your project's package.json file. This helps maintain consistency and ensures that others working on the project use the correct npm version.

To optimize the serving of static files in Express.js, enabling caching is a common practice.

  • Compression
  • Minification
  • Preloading
  • Caching
Enabling caching is a common practice in Express.js to optimize the serving of static files. Caching allows the server to store and reuse static files, reducing the need to fetch them from the disk or generate them on each request, thus improving performance.

You are tasked with creating an HTTP proxy server using the http module. What steps and considerations would you take to handle incoming requests and forward them to the appropriate destination?

  • Parse the incoming request, extract the destination URL, and create a new request to the destination server.
  • Directly pass the incoming request to the destination server without any parsing.
  • Block all incoming requests as proxy servers should only send responses.
  • Use the 'fs' module to read and forward the request to the destination.
To create an HTTP proxy server, you should parse the incoming request, extract the destination URL, and create a new request to the destination server. The other options are not suitable for proxy server implementations.

To specify a specific version of a package in the package.json file, you can use a ______ to define the exact version number.

  • semver
  • specifier
  • version-lock
  • exact
To specify a specific version of a package in the package.json file, you can use a semver specifier to define the exact version number or a range of versions you want to use. Semver specifiers allow for flexibility in specifying version requirements for packages.

In a content delivery network (CDN), the process of distributing copies of files to multiple geographically dispersed servers is known as ______.

  • Load Balancing
  • Replication
  • Caching
  • Clustering
In a content delivery network (CDN), the process of distributing copies of files to multiple geographically dispersed servers is known as "Replication." This ensures that content is readily available to users from nearby servers, reducing latency and improving load times. Load balancing, caching, and clustering are related concepts but not specific to CDN replication.

Which caching strategy involves keeping the most recently used items?

  • LRU (Least Recently Used)
  • FIFO (First In, First Out)
  • Random Replacement
  • LFU (Least Frequently Used)
The caching strategy that involves keeping the most recently used items is called LRU, which stands for "Least Recently Used." In LRU caching, when the cache is full and needs to make space for a new item, it removes the least recently accessed item. This strategy is often used to maximize cache efficiency.

How can you allocate a buffer of a specified size without initializing it in Node.js?

  • Buffer.alloc(size)
  • Buffer.create(size)
  • Buffer.new(size)
  • Buffer.allocate(size)
To allocate a buffer of a specified size without initializing it in Node.js, you should use the Buffer.alloc(size) method. It allocates a new buffer of the given size and initializes it with zeros. The other options are not valid methods for achieving this.

You need to develop a function that takes an array of numbers and returns a new array containing only the unique numbers. What approach would you use to filter out the duplicates?

  • Using a for loop and checking uniqueness manually
  • Using Array.prototype.filter() and a custom filter function
  • Using Array.from(new Set(array))
  • Using Array.prototype.reduce() with a custom accumulator function
To filter out duplicate numbers in an array, you can use Array.from(new Set(array)). This creates a Set from the array (which only keeps unique values) and then converts it back to an array. It's a concise and efficient approach for this task.

What considerations should be made when implementing transactions in Sequelize for isolation and atomicity?

  • Choosing the appropriate isolation level
  • Ensuring that transactions are started manually
  • Avoiding the use of nested transactions
  • Setting the transaction mode to 'autocommit'
When implementing transactions in Sequelize for isolation and atomicity, you should consider choosing the appropriate isolation level based on your application's requirements. Transactions should typically be started manually, avoiding nested transactions for simplicity and maintainability. Setting the transaction mode to 'autocommit' would defeat the purpose of using transactions for atomicity.

When using a third-party storage service to store uploaded files, what is crucial to prevent unauthorized access?

  • Use predictable file names and URLs for easy access.
  • Share access credentials widely to simplify sharing files.
  • Implement proper access controls and use signed URLs or tokens.
  • Store files without any access restrictions for maximum accessibility.
When using a third-party storage service, it's crucial to prevent unauthorized access by implementing proper access controls and using mechanisms like signed URLs or tokens. This ensures that only authorized users can access the files while keeping them secure. Using predictable file names and URLs, sharing access credentials widely, or storing files without restrictions can lead to unauthorized access and security breaches.

In what scenario would using Domain API be beneficial for error handling in Node.js?

  • When handling HTTP requests in Express.js.
  • When handling file I/O operations.
  • When creating a RESTful API.
  • When dealing with database connections.
The Domain API was deprecated in Node.js and is no longer recommended for use. It was originally designed to handle errors in scenarios like file I/O operations. However, it has been deprecated because it didn't provide a robust solution for error handling, and other mechanisms like try...catch and event listeners have become more standard for handling errors in modern Node.js applications. Using it is not advisable in any scenario.