Express Flashcards

1
Q

Why do we use Express instead of Node?

A

Using vanilla Node.js is far too complex. Instead of focusing on these complex tasks, we want to concentrate on our business logic. To achieve this, we use frameworks like Express. They provide an easier way to create our server logic instead of writing every single detail ourselves.

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

Is Express installed as a production or developer dependency?

A

Express needs to be installed as a production dependency, not just for development.

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

When we import Express, what does it return?

A

Express function that is returned when importing Express. This function holds the configuration and data for our server. Additionally, it serves as a valid handler for the createServer function.

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

What is Middleware?

A

Middleware consists of functions that incoming requests pass through. This means you can add many functions through which the request can go until you send a response.

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

What does Middleware allow us to do?

A

This allows us to split our code and use third-party packages.

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

What can third party packges give us?

A

These third-party packages provide middleware functions that allow you to use their functionalities. This is a core concept in Express.

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

How do we create Middleware?

A

We can create a middleware function by using the use method on the app instance we get from importing Express.

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

What does the use method take as an argument?

A

In the use method, we pass in a function that accepts three arguments: request, which allows us to work with the request object; response, which allows us to work with the response object; and next, which is not an object but a function. This next function allows us to proceed to the next middleware.

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

How can we send a response?

A

We can use the send method on the response object. This method allows us to send a body of any type, and Express will automatically set the appropriate content type.

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

How do we create a server using Express? How does it work? What do we no longer need?

A

To listen to a server by calling the listen method on the app object we received from Express. This method essentially does what we did before when we created a server and listened on a specific port. However, instead of writing all that code, we can simply call the listen method on the app object with the port as an argument, and it creates and listens to the server for us. We no longer need to import the HTTP module.

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

When adding a path to the use method, what is the default path?

A

The default path is the /

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

When the path is / or /message. What does this mean? Why should the / path always be at the bottom?

A

The default path is ‘/’. The default path does not mean that the full path has to match it exactly, but that the path needs to begin with it. Since middleware is processed in the order it is added, we should place the ‘/’ path middleware at the bottom. If it is at the top or middle, all paths start with ‘/’, so it will intercept requests before they reach their designated handlers.

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

What would you need to parse data from the request body?

A

You need to install bodyparser from NPM. Than, import body-parser and create a middleware function at the top of your middleware stack. This ensures the data gets parsed before reaching your other middleware. To handle URL-encoded data, you can use bodyParser.urlencoded({ extended: false }). This creates a middleware function that will parse the data and then pass it to the next middleware. However, you can also use express.json().

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

How can we handle POST or GET request?

A

To handle POST request we use the post method.
To handle GET request we use the get method.

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

What is the purpose of the routes folder?
What will it contain?

A

To organize our code in Node.js, we create a routes folder. This folder will contain an admin file for admin-specific routes and another file for routes accessible to the client.

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

When we import express in our routes folder, what do we do differently?

A

Instead of using app, we create a new variable called router. This router variable will hold an object returned by calling express.Router(). This is like a mini Express app that is connected to our main Express application.

17
Q

What is the difference between using use and something specific like get?

A

Using the GET or POST methods does not work like the USE method. GET and POST use exact matches. So, we don’t have to worry about the order of our middleware, but it’s still nice to keep our middleware organized.

18
Q

When we have routes that all have a similar starting path what can we do?

What should we watch out for when doing this?

A

Instead of writing the common prefix every single time, we can omit it and place the common starting file path into a middleware that will handle all related requests.

However, keep in mind that although we omit the common prefix in the middleware file, when we need to send a request to a specific endpoint, we must include the full starting path.

19
Q

How can we make a helper function to help us get the root directory?

How do we import into other files?

Why is it important?

A

Firstly, we need to create a new folder in our project called “utils”. In this folder, we create a new file called “paths”. This file will be the one that handles getting our project directory. In this file, we import the path module. After importing, we export the path module. However, we call the dirname method on the object that holds our path object. In this method, we pass require.main.filename, which will construct the file path to our current main project folder. We can now use this newly created value, which is the file path to our main project, in other files where we need to send files.

We first import the file by assigning the variable “rootfile” to ‘../utils/path’. Once imported, instead of __dirname, we simply replace it with “rootfile”.

This is helpful because we no longer need to traverse our file system. We can instantly go to the main folder, and then we can navigate into the folder we would like. Whereas __dirname would take you to the file it was called in.

20
Q
A