When designing schemas for large-scale systems, the principle of ________ involves dividing the dataset into smaller, more manageable parts.
- Sharding
- Normalization
- Aggregation
- Indexing
The principle of "sharding" involves dividing the dataset into smaller, more manageable parts, typically based on a certain criteria like range or hash, to distribute data across multiple servers or nodes in a distributed system. Sharding is a crucial concept in achieving horizontal scalability in databases.
When configuring the static files directory in Express.js, the path specified is relative to the ______ directory.
- current
- project
- root
- parent
When configuring the static files directory in Express.js, the path specified is relative to the root directory of your project. This means that you specify the path from the root of your project's directory structure.
For optimal performance when querying a large dataset in a NoSQL database, it is crucial to have proper ______ in place.
- Indexing
- Sharding
- Versioning
- Compression
For optimal performance when querying a large dataset in a NoSQL database, it is crucial to have proper indexing in place. Indexes help the database quickly locate and retrieve the data, reducing query response times.
You are developing a web application that needs to make API requests to a server on a different domain. How would you handle CORS to ensure that your web application can interact with the server without any issues?
- Set up a server-side proxy to forward requests to the other domain.
- Enable CORS on the server by adding appropriate headers to allow cross-origin requests.
- Use JSONP for making cross-domain requests.
- Disable CORS restrictions in the browser settings.
The correct approach is to enable CORS on the server by adding the appropriate headers that allow cross-origin requests. Option A can be a workaround, but it's not the best practice. Option C is an outdated method, and option D is not a recommended security practice.
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 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.
When performing CRUD operations on a database, which operation can be the most expensive in terms of performance?
- Create
- Read
- Update
- Delete
Among CRUD operations, "Update" can often be the most expensive in terms of performance. This is because updating records may require the database to search for the existing record, make changes, and write the updated data back to disk, which can be resource-intensive. Read operations are typically less expensive.
When a Promise is pending and neither fulfilled nor rejected, it is in the ________ state.
- awaiting
- undefined
- completed
- idle
When a Promise is pending and hasn't been resolved or rejected, it is in the "awaiting" state. This is the initial state of a Promise before it is settled.
What are the best practices for error handling in a large-scale Node.js application?
- Avoiding error handling altogether for performance reasons.
- Using global error handlers for unhandled errors.
- Handling errors at the lowest level of code execution.
- Logging errors but not taking any action.
In large-scale Node.js applications, best practices for error handling include handling errors at the lowest level of code execution, close to where they occur. This improves code maintainability and makes it easier to trace the source of errors. Avoiding error handling for performance reasons is a bad practice. Using global error handlers is suitable for unhandled errors, but it's essential to handle errors at the source first. Simply logging errors without taking corrective action is incomplete error handling.
You are building a RESTful API with Express to serve a mobile application. The mobile development team has asked for the ability to retrieve condensed responses to minimize data usage. How would you accommodate this request while maintaining the integrity of your API?
- Create separate endpoints for condensed and full responses.
- Use query parameters to allow clients to specify the response format.
- Disable compression to send smaller payloads.
- Use WebSocket instead of REST for real-time updates.
Using query parameters to allow clients to specify the response format is a common and RESTful approach to accommodating different client needs. Creating separate endpoints for each format can lead to redundancy and maintenance challenges. Disabling compression would likely increase, not decrease, data usage. Using WebSockets is for real-time communication and doesn't directly address response format concerns.