In Express.js, middleware functions have access to the request object, the response object, and the ______ function.
- next()
- send()
- app()
- router()
In Express.js, middleware functions have access to the request object (req), the response object (res), and a callback function commonly named next(). The next() function is used to pass control to the next middleware function in the chain.
Which of the following is the primary goal of input sanitization?
- Enhancing user experience.
- Ensuring data accuracy.
- Preventing cross-site scripting (XSS) attacks.
- Optimizing database performance.
The primary goal of input sanitization is to prevent cross-site scripting (XSS) attacks. It involves removing or encoding potentially dangerous characters from user input to ensure that it cannot be executed as script on a web page, thus enhancing web security.
Which of the following is an example of an Object Document Mapper (ODM) for MongoDB in Node.js?
- Mongoose
- Sequelize
- Knex
- TypeORM
Mongoose is a popular Object Document Mapper (ODM) for MongoDB in Node.js. It provides a structured way to interact with MongoDB, allowing developers to define schemas and models for their data. The other options, Sequelize, Knex, and TypeORM, are primarily used with relational databases and are not ODMs for MongoDB.
When dealing with CORS, the Access-Control-Allow-Credentials header should be set to true to allow ________ to be included in the request.
- cookies
- headers
- authentication
- origins
When dealing with CORS, the Access-Control-Allow-Credentials header should be set to true to allow cookies to be included in cross-origin requests. This is necessary when you want to make authenticated requests across origins.
You are developing a system with stringent data integrity requirements. How would you design the schema to enforce data integrity constraints and handle violations effectively?
- Use database triggers to enforce constraints.
- Implement application-level validation only.
- Apply foreign key constraints to maintain data relationships.
- Rely solely on user input validation.
Option (1) is a valid approach using database triggers to enforce constraints. Option (2) shouldn't be the sole method, as application-level validation can be bypassed. Option (3) is essential, especially for maintaining data relationships. Option (4) is not sufficient for ensuring data integrity on its own.
How can developers handle multiple callback functions to avoid "Callback Hell" in Node.js?
- Nest callbacks within each other for better organization.
- Use async/await to write asynchronous code more sequentially.
- Avoid callbacks altogether and use Promises exclusively.
- Increase the event loop's capacity for handling callbacks.
To avoid "Callback Hell" in Node.js, developers can use async/await, which allows them to write asynchronous code in a more sequential and readable manner. This approach reduces the nesting of callbacks and makes the code easier to maintain.
The fs.watch method is used to watch for changes in a file or a directory but may not be consistent across platforms due to its reliance on ______.
- inotify
- polling
- event-driven
- file system events
The fs.watch method relies on polling on some platforms to detect file and directory changes. This polling approach may not be consistent across platforms and can have performance implications.
In a complex CORS scenario, how can you selectively allow certain types of requests while denying others?
- Create custom middleware
- Set Access-Control-Allow-Origin to *
- Use the OPTIONS method
- Configure server-side routing
In complex CORS scenarios, you can selectively allow or deny requests by creating custom middleware on your server. This middleware can inspect the request headers, methods, or other criteria to determine whether to allow or deny a request. The other options are components of CORS but do not provide fine-grained control over request types.
________ is a security practice that involves encoding information so that only authorized parties can access it.
- Encryption
- Hashing
- Salting
- Obfuscation
Encryption is a security practice that involves encoding information in a way that only authorized parties with the decryption key can access it. This is commonly used to protect sensitive data during transmission and storage.
In a system following eventual consistency, what implications does it have on Read operations after a Write operation?
- Strong consistency
- Read operations may return stale data
- Write operations are blocked until consistency is achieved
- Write operations are delayed
In a system with eventual consistency, Read operations may return stale or outdated data for a period of time after a Write operation. This is because eventual consistency prioritizes availability and performance over strict consistency. Strong consistency (Option 1) ensures that all reads return the most recent write but may lead to higher latency. Options 3 and 4 are not characteristic of eventual consistency.