How can you handle errors in middleware in Express.js?
- Errors in middleware are automatically handled by Express.js and sent as a default error response.
- Use the next() function with an error object to pass it to an error handling middleware.
- Errors in middleware should be caught with a try...catch block inside the middleware function.
- Express.js does not support error handling in middleware.
In Express.js, errors in middleware can be handled by calling next(error) to pass the error to an error handling middleware. This allows for centralized error handling.
How can middleware functions modify the request and response objects in Express.js?
- Middleware functions can modify request and response objects directly.
- Middleware functions cannot modify request and response objects in Express.js.
- Middleware functions can only modify the request object, not the response object.
- Middleware functions can only modify the response object, not the request object.
Middleware functions in Express.js have access to the request and response objects, and they can modify these objects directly. This allows them to add, modify, or remove properties and headers, which is useful for various purposes like authentication, logging, and data transformation.
Which of the following databases is a NoSQL database?
- MySQL
- SQLite
- MongoDB
- PostgreSQL
MongoDB is a NoSQL database, known for its flexibility in handling unstructured or semi-structured data. It uses a document-oriented data model, making it a popular choice for applications that require dynamic, schema-less data storage. MySQL, SQLite, and PostgreSQL are all SQL databases, which follow a structured, table-based data model.
You have to deploy a Node.js application, but the production environment does not allow internet access, preventing npm packages from being installed. How do you prepare and install the necessary npm packages in such an environment?
- Download packages manually and copy them to the production server
- Use a proxy server to allow internet access for npm
- Bundle all npm packages with your application during development
- Ask the production environment to whitelist npm's servers
To install npm packages in an environment without internet access, you can download packages manually and copy them to the production server. This approach ensures the necessary dependencies are available. Using a proxy server or whitelisting npm's servers may not always be feasible.
In JavaScript, variables declared using the var keyword are hoisted to the top of their ________.
- scope
- function
- declaration
In JavaScript, variables declared using the var keyword are hoisted to the top of their containing function's scope. This means that they are moved to the top of the function during the compilation phase, allowing you to use them before they are declared in the code.
How can you handle errors in a readable stream in Node.js?
- stream.on('error', (err) => { /* Handle error */ });
- stream.catch((err) => { /* Handle error */ });
- stream.error((err) => { /* Handle error */ });
- try { /* Stream operations */ } catch (err) { /* Handle error */ }
In Node.js, you can handle errors in a readable stream by listening to the 'error' event using stream.on('error', (err) => { /* Handle error */ });. The other options are not the correct way to handle errors in streams.
When using await inside a function, what does the function return?
- It returns the resolved value of the awaited Promise.
- It returns a boolean indicating if the Promise is pending.
- It returns an array of all pending Promises.
- It returns an error if the Promise is rejected.
When you use await inside an async function, the function returns the resolved value of the awaited Promise. This enables you to work with asynchronous code in a more synchronous style.
What is the significance of HTTP/2 in web performance optimization compared to HTTP/1.x?
- HTTP/2 uses multiplexing to allow multiple requests and responses to be sent in parallel over a single connection.
- HTTP/2 is a more secure version of HTTP/1.x.
- HTTP/2 reduces the need for server-side caching.
- HTTP/2 requires fewer resources on the client-side compared to HTTP/1.x.
HTTP/2 significantly improves web performance compared to HTTP/1.x by introducing features like multiplexing, which allows multiple requests and responses to be sent concurrently over a single connection. This reduces latency and speeds up web page loading. The other options are not accurate representations of HTTP/2's benefits.
In schema design, the property that ensures that only valid data is stored in the database is known as ________.
- Validation
- Verification
- Constraints
- Authentication
In schema design, the property that ensures that only valid data is stored in the database is known as "Constraints." Constraints include rules such as unique keys, primary keys, foreign keys, and check constraints that enforce data integrity and validity.
JSON Web Tokens (JWT) are composed of three parts: a header, a payload, and a ______.
- signature
- key
- footer
- token
JSON Web Tokens (JWTs) are composed of three parts: a header, a payload, and a signature. The signature is used to verify the authenticity of the token and ensure that it has not been tampered with. The other options are not components of JWTs.