Express CH3 Flashcards

(59 cards)

1
Q

What are the four main features of Express?

A
  • 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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What does Node’s built-in http module allow you to do?

A

Build an HTTP server that responds to HTTP requests from browsers

This module is essential for creating web applications using Node.js.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is Express in relation to Node?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is middleware in Express?

A

An array of functions that process requests in sequence

Middleware allows multiple operations to be performed on a request before sending a response.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

How does routing in Express work?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What does Express extend in terms of request and response objects?

A

Extra methods and properties for developer convenience

These extensions help streamline the development process.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is the primary purpose of using middleware?

A

To decompose tasks into separate functions for better organization

This improves code readability and maintainability.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What happens if no middleware responds to a request?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Fill in the blank: Middleware is present in other web application frameworks like ______.

A

Python’s Django or PHP’s Laravel

The concept of middleware is widely adopted across different programming languages and frameworks.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is a common use of logging middleware?

A

To log all requests before passing them to the next middleware

This is useful for monitoring and debugging applications.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

True or False: Middleware can only modify the response object.

A

False

Middleware can modify both the request and response objects.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What does the following middleware do: function myFunMiddleware(request, response, next) { ... next(); }?

A

It does nothing but calls the next middleware in the chain

This is an example of a passive middleware function.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What is the purpose of the next() function in middleware?

A

To pass control to the next middleware function in the stack

It is crucial for maintaining the flow of request processing.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

How is authentication implemented in the example provided?

A

By checking if the current minute is even

This is a simplified and artificial example of an authentication mechanism.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What does app.use() do in an Express application?

A

Registers middleware functions for handling requests

This is how middleware is integrated into the request handling pipeline.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What does the response.statusCode = 403; line signify?

A

It sets the HTTP response status to ‘Forbidden’

This indicates to the client that they do not have permission to access the requested resource.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

What is the first middleware in the specified order?

A

Logging middleware

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

What does Morgan do in an Express application?

A

Morgan is a logging middleware that provides logging features.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

Why are loggers useful in web applications?

A

They help see user activity, diagnose crashes, and perform performance analysis.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

What command is used to install Morgan?

A

npm install morgan –save

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q

What is express.static used for?

A

It helps serve static files like images, CSS, or HTML.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
22
Q

What method is used to set up the public path for serving static files?

A

path.resolve

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
23
Q

What happens if no matching file exists in the public folder?

A

The next middleware will be called.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
24
Q

What is the function of connect-ratelimit middleware?

A

It throttles connections to limit the number of requests per hour.

25
What does Helmet middleware do?
It adds HTTP headers to enhance security against attacks.
26
What does cookie-parser middleware do?
It parses browser cookies.
27
What is the purpose of response-time middleware?
It sends the X-Response-Time header for performance debugging.
28
How does routing work in Express?
It maps requests to specific handlers based on URL and HTTP verb.
29
What method is used to define a GET route for the homepage?
app.get('/')
30
What is the purpose of req.params in Express routing?
It extracts parameters from the URL.
31
What does the response.redirect method do?
It redirects the client to a different URL.
32
What method does Express provide for sending a file?
response.sendFile
33
What does request.ip return?
It returns the IP address of the incoming request.
34
What is the purpose of the middleware that blocks an evil IP address?
To prevent requests from specific IP addresses.
35
What are views in the context of Express applications?
Dynamically generated HTML served by the server.
36
What templating engine is used in the examples?
EJS (Embedded JavaScript)
37
What command is used to install EJS?
npm install ejs --save
38
What does <%= message %> do in an EJS file?
It interpolates the variable 'message' into the HTML.
39
How is a view rendered in Express?
Using the response.render method.
40
What is the result of rendering an EJS file with a variable?
It generates HTML that includes the value of the variable.
41
What does EJS stand for?
Embedded JavaScript
42
What is the primary purpose of EJS?
To render dynamic HTML pages
43
What is a guestbook in the context of web applications?
An online application where users can write new entries and browse others' entries
44
What two pages does the guestbook application consist of?
* A homepage listing all guestbook entries * A page with an 'add new entry' form
45
What is the command to create a new Node.js project with a package.json file?
npm init
46
What is the purpose of the body-parser module in the guestbook application?
To parse the body of HTTP POST requests
47
What does the following line of code do? app.use(bodyParser.urlencoded({ extended: false }));
Populates req.body with form data submitted by the user
48
What HTTP method is used to submit new entries in the guestbook?
POST
49
What does the app.post('/new-entry') function do?
* Checks if title and body are provided * Adds new entry to the entries array * Redirects to the homepage
50
What is the purpose of the middleware function logger in the app?
To log all incoming requests for debugging
51
What is the default port number the guestbook application listens on?
3000
52
What does the app.use(function(request, response) {...}) function handle?
It renders a 404 page for unknown routes
53
What does the views folder contain in the guestbook application?
* header.ejs * footer.ejs * index.ejs * new-entry.ejs * 404.ejs
54
What is the purpose of the header.ejs file?
To define the common header that appears at the top of every page
55
What CSS framework is used in the guestbook application?
Twitter Bootstrap
56
Fill in the blank: Express sits on top of Node's ______ functionality.
HTTP
57
True or False: Express has a middleware feature that allows you to pipeline a single request through multiple functions.
True
58
What is the function of Express's routing feature?
To map certain HTTP requests to specific functionalities
59
What does the EJS templating engine allow you to do?
Dynamically create HTML content