Skip to content

Commit

Permalink
Apply suggestions from code review
Browse files Browse the repository at this point in the history
Co-authored-by: 01zulfi <85733202+01zulfi@users.noreply.github.com>
  • Loading branch information
fcasibu and 01zulfi committed May 11, 2024
1 parent 818ecec commit cc01fa5
Showing 1 changed file with 4 additions and 4 deletions.
8 changes: 4 additions & 4 deletions nodeJS/express/controllers.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ A middleware function can perform various tasks, such as:
- Calling the next middleware function in the chain.
- Ending the request-response cycle (meaning no further middleware functions are called, even if there are more in the chain).

Express has a rich ecosystem and you will likely find a package that solves the problem you are encountering. For example, some packages provide middleware functions to handle authentication, cors, rate limiting, sessions, logging, validation, and more! But we should identify the basic and built-in ones first.
Express has a rich ecosystem and you will likely find a package that solves the problem you are encountering. For example, some packages provide middleware functions to handle authentication, cors, rate limiting, sessions, logging, validation, and more! Throughout this lesson (and the course), we'll be introducing middlewares that would be required to build projects in this course. Although, you're welcome to explore on your own.

#### Application-level middleware

Expand Down Expand Up @@ -102,7 +102,7 @@ function myMiddleware(req, res, next) {
app.use(myMiddleware);
```

In this example, the middleware function logs a message, adds a custom property to the request object, and then calls the next() function to pass control to the next middleware function or route handler. We also register the middleware function through the usage of `app.use` which makes this an application-level middleware. Other middleware functions below this one will now be able to see a property `customProperty` with the value `"Hello from myMiddleware"`.
In this example, the middleware function logs a message, adds a custom property to the request object, and then calls the `next()` function to pass control to the next middleware function or route handler. We also register the middleware function through the usage of `app.use` which makes this an application-level middleware. Middleware functions following `myMiddleware` in this chain can now access `req.customProperty` with the value `"Hello from myMilddleware"`.

One thing to note about is that middleware functions are executed in the order they are defined or registered in your application. This means that the sequence in which you define your middleware functions matters, as it determines the order in which they will be invoked during the request-response cycle. So you need to make sure and be aware that your middleware fnuctions are placed in the correct order. As an example, some packages have middleware functions that changes the `Request` object, and as a result, these middleware functions should be placed at the very top of your application in order for you to be able to see their changes in all of your middleware functions below it.

Expand Down Expand Up @@ -135,7 +135,7 @@ const getUserById = async (req, res) => {
};
```

In this example, the `getUserById` function is a controller that handles a specific action related to retrieving a user by their ID. Let's break down what's happening in this controller:
In this example, the `getUserById` function is a controller that handles a specific action related to retrieving a user by their ID. You'll use this controller by importing it into file where routes are defined, and using it like so `router.get("/user/:id", getUserById)`. Let's break down what's happening in this controller:

1. The controller extracts the userId from the request parameters (`req.params.id`). This assumes that the parameter is defined with a route, such as `/users/:id`.
1. It then invokes a database query function `someDBQueryToGetUser` to retrieve the user data based on the userId.
Expand Down Expand Up @@ -321,7 +321,7 @@ Out of the four, you will likely only use the first two, unless you have a very

### Consolidating what we've learned

A basic example of the file structure would be:
Following is the folder structure you'd end up with if you have been coding along:

```text
express-app/
Expand Down

0 comments on commit cc01fa5

Please sign in to comment.