Express CH3 Flashcards
(59 cards)
What are the four main features of Express?
- Middleware for letting a request flow through multiple headers
- Routing for handling a request at a specific spot
- Convenience methods and properties
- Views for dynamically rendering HTML
Each feature serves to enhance the functionality and usability of web applications built with Express.
What does Node’s built-in http module allow you to do?
Build an HTTP server that responds to HTTP requests from browsers
This module is essential for creating web applications using Node.js.
What is Express in relation to Node?
An abstraction layer on top of Node’s built-in HTTP server
Express simplifies the process of building web servers compared to using Node’s http module directly.
What is middleware in Express?
An array of functions that process requests in sequence
Middleware allows multiple operations to be performed on a request before sending a response.
How does routing in Express work?
Functions are called only when visiting a specific URL with a specific HTTP method
This allows for tailored responses based on the route accessed by the user.
What does Express extend in terms of request and response objects?
Extra methods and properties for developer convenience
These extensions help streamline the development process.
What is the primary purpose of using middleware?
To decompose tasks into separate functions for better organization
This improves code readability and maintainability.
What happens if no middleware responds to a request?
The server will hang and the browser will not receive a response
It is essential for at least one middleware to respond to avoid hanging requests.
Fill in the blank: Middleware is present in other web application frameworks like ______.
Python’s Django or PHP’s Laravel
The concept of middleware is widely adopted across different programming languages and frameworks.
What is a common use of logging middleware?
To log all requests before passing them to the next middleware
This is useful for monitoring and debugging applications.
True or False: Middleware can only modify the response object.
False
Middleware can modify both the request and response objects.
What does the following middleware do: function myFunMiddleware(request, response, next) { ... next(); }
?
It does nothing but calls the next middleware in the chain
This is an example of a passive middleware function.
What is the purpose of the next()
function in middleware?
To pass control to the next middleware function in the stack
It is crucial for maintaining the flow of request processing.
How is authentication implemented in the example provided?
By checking if the current minute is even
This is a simplified and artificial example of an authentication mechanism.
What does app.use()
do in an Express application?
Registers middleware functions for handling requests
This is how middleware is integrated into the request handling pipeline.
What does the response.statusCode = 403;
line signify?
It sets the HTTP response status to ‘Forbidden’
This indicates to the client that they do not have permission to access the requested resource.
What is the first middleware in the specified order?
Logging middleware
What does Morgan do in an Express application?
Morgan is a logging middleware that provides logging features.
Why are loggers useful in web applications?
They help see user activity, diagnose crashes, and perform performance analysis.
What command is used to install Morgan?
npm install morgan –save
What is express.static used for?
It helps serve static files like images, CSS, or HTML.
What method is used to set up the public path for serving static files?
path.resolve
What happens if no matching file exists in the public folder?
The next middleware will be called.
What is the function of connect-ratelimit middleware?
It throttles connections to limit the number of requests per hour.