Express Flashcards
Why do we use Express instead of Node?
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.
Is Express installed as a production or developer dependency?
Express needs to be installed as a production dependency, not just for development.
When we import Express, what does it return?
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.
What is Middleware?
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.
What does Middleware allow us to do?
This allows us to split our code and use third-party packages.
What can third party packges give us?
These third-party packages provide middleware functions that allow you to use their functionalities. This is a core concept in Express.
How do we create Middleware?
We can create a middleware function by using the use method on the app instance we get from importing Express.
What does the use method take as an argument?
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 can we send a response?
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 do we create a server using Express? How does it work? What do we no longer need?
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.
When adding a path to the use method, what is the default path?
The default path is the /
When the path is / or /message. What does this mean? Why should the / path always be at the bottom?
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.
What would you need to parse data from the request body?
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 can we handle POST or GET request?
To handle POST request we use the post method.
To handle GET request we use the get method.
What is the purpose of the routes folder?
What will it contain?
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.