In Node.js, '______' is used to signify the end of a writable stream.

  • finish
  • close
  • end
  • complete
In Node.js, the 'end' event is used to signify the end of a writable stream. This event is emitted when all data has been written to the stream and it's safe to end it. The other options (finish, close, complete) are not typically used for this purpose.

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.

How can you handle error responses in Express for cleaner error reporting?

  • Using the throw statement
  • Using the try...catch block
  • Implementing custom error handling middleware
  • Ignoring errors for cleaner logs
To handle error responses in Express for cleaner error reporting, you can implement custom error handling middleware. This middleware can catch errors and provide a standardized way to handle and respond to them, improving error reporting and debugging.

How can you match routes with a specific pattern in Express.js?

  • app.pattern('/route', callback)
  • app.match('/route', callback)
  • app.use('/route', callback)
  • app.get('/route', callback)
In Express.js, you can use the app.get('/route', callback) method to match routes with a specific pattern. The get method is used to handle HTTP GET requests and is often used to define routes with specific patterns. The other options do not represent the standard way to define route patterns in Express.js.

To handle HTTP POST requests in Express, you would use the ______ method.

  • GET
  • POST
  • PUT
  • DELETE
To handle HTTP POST requests in Express, you would use the POST method. The POST method is used for submitting data to be processed to a specified resource.

How does Content Security Policy (CSP) help in preventing XSS attacks, and what are its limitations?

  • CSP restricts the usage of third-party libraries
  • CSP blocks all JavaScript execution
  • CSP defines rules for resource loading and script execution
  • CSP is ineffective against XSS attacks
Content Security Policy (CSP) helps prevent Cross-Site Scripting (XSS) attacks by defining rules for resource loading and script execution in web applications. It can restrict the sources from which scripts can be executed, mitigating the risk of malicious script injection. However, CSP has limitations, such as its inability to prevent all forms of XSS attacks, the need for careful configuration, and potential compatibility issues with legacy code.