Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Node revamp: Controllers Lesson #27695

Open
wants to merge 17 commits into
base: main
Choose a base branch
from

Conversation

fcasibu
Copy link
Contributor

@fcasibu fcasibu commented Mar 26, 2024

Because

To add a new lesson about Controllers in the MVC pattern for the node revamp

This PR

  • Adds new lesson to nodeJS/express/controllers.md

Issue

Closes #27615

Additional Information

Pull Request Requirements

  • I have thoroughly read and understand The Odin Project Contributing Guide
  • The title of this PR follows the location of change: brief description of change format, e.g. Intro to HTML and CSS lesson: Fix link text
  • The Because section summarizes the reason for this PR
  • The This PR section has a bullet point list describing the changes in this PR
  • If this PR addresses an open issue, it is linked in the Issue section
  • If any lesson files are included in this PR, they have been previewed with the Markdown preview tool to ensure it is formatted correctly
  • If any lesson files are included in this PR, they follow the Layout Style Guide

@github-actions github-actions bot added the Content: NodeJS Involves the NodeJS course label Mar 26, 2024
@fcasibu fcasibu force-pushed the node-revamp/nodejs-controllers-lesson branch 7 times, most recently from 4bd9aab to f17bd6d Compare March 27, 2024 04:01
@fcasibu fcasibu force-pushed the node-revamp/nodejs-controllers-lesson branch from f17bd6d to 0b67561 Compare March 27, 2024 05:11
@fcasibu fcasibu force-pushed the node-revamp/nodejs-controllers-lesson branch from bec9a1a to f449a83 Compare March 27, 2024 05:24
@01zulfi 01zulfi self-requested a review March 27, 2024 09:29
@01zulfi 01zulfi added the Project Node Revamp Issues/PRs related to the Node Revamp project label Mar 27, 2024
@01zulfi 01zulfi self-assigned this Mar 28, 2024
@fcasibu fcasibu force-pushed the node-revamp/nodejs-controllers-lesson branch 4 times, most recently from 0bd8e04 to 0cc9f09 Compare March 31, 2024 07:53
@fcasibu fcasibu force-pushed the node-revamp/nodejs-controllers-lesson branch from 0cc9f09 to bc5e301 Compare April 1, 2024 06:26
Copy link
Contributor

@MaoShizhong MaoShizhong left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mainly wording/formatting suggestions. It's a big lesson and I've not quite got the time to dive deeper into the rest of it, but will aim to do so at a later point.

nodeJS/express/controllers.md Outdated Show resolved Hide resolved
nodeJS/express/controllers.md Outdated Show resolved Hide resolved
nodeJS/express/controllers.md Outdated Show resolved Hide resolved
nodeJS/express/controllers.md Outdated Show resolved Hide resolved
nodeJS/express/controllers.md Outdated Show resolved Hide resolved
nodeJS/express/controllers.md Outdated Show resolved Hide resolved
nodeJS/express/controllers.md Outdated Show resolved Hide resolved
nodeJS/express/controllers.md Outdated Show resolved Hide resolved
nodeJS/express/controllers.md Outdated Show resolved Hide resolved
nodeJS/express/controllers.md Outdated Show resolved Hide resolved
fcasibu and others added 2 commits April 2, 2024 10:33
Co-authored-by: MaoShizhong <122839503+MaoShizhong@users.noreply.github.com>
@fcasibu fcasibu force-pushed the node-revamp/nodejs-controllers-lesson branch from 776aaea to 48196ad Compare April 2, 2024 02:35
@fcasibu
Copy link
Contributor Author

fcasibu commented Apr 2, 2024

Thanks @MaoShizhong I appreciate it!

Copy link
Contributor

@MaoShizhong MaoShizhong left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Again, mainly nits and suggestions.

Content-wise, fabulous lesson 🤌

Some general things that likely should be changed throughout the file:

  • End list items with a period to match most other lessons.
  • Capitalise Express, as opposed to express.
  • Change "middleware"/"middlewares" to "middleware function" and "middleware functions" respectively, to follow the terminology used by Express itself. The few times Express use "middleware" without "function(s)" after it, it's referring to the general concept of middleware. For example, "application-level middleware" (referring the the middleware functions that are application-level as a whole" or "types of middleware", again being a more "conceptual" use of the word. Whenever it refers to actual functions, it uses "middleware function".

nodeJS/express/controllers.md Outdated Show resolved Hide resolved
nodeJS/express/controllers.md Outdated Show resolved Hide resolved

#### Application-level middleware

Application-level middleware are bound to an *instance of Express* using the app.use or using app.METHOD (e.g. app.get, app.post) functions. These are middlewares that are executed in **every incoming requests** to the application with the specified path `/` being the default mount path (mount path can be changed by adding it as the first argument), but if the request-response cycle ends before even getting to the specific middleware then it will of course not run, but typically these middlewares are placed on top of your application code to ensure they always run first. Very common built-in middlewares that you will likely use are:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Application-level middleware are bound to an *instance of Express* using the app.use or using app.METHOD (e.g. app.get, app.post) functions. These are middlewares that are executed in **every incoming requests** to the application with the specified path `/` being the default mount path (mount path can be changed by adding it as the first argument), but if the request-response cycle ends before even getting to the specific middleware then it will of course not run, but typically these middlewares are placed on top of your application code to ensure they always run first. Very common built-in middlewares that you will likely use are:
Application-level middleware are bound to an *instance of Express* using `app.use` or using `app.<METHOD>` (e.g. `app.get`, `app.post`) functions. These are middleware functions that are executed in **every incoming request** to the application with the specified path `/` being the default mount path (mount path can be changed by adding it as the first argument), but if the request-response cycle ends before even getting to the specific middleware then it will of course not run, but typically these middlewares are placed on top of your application code to ensure they always run first. Very common built-in middlewares that you will likely use are:

The first bit just has a few punctuation bits.
The second sentence ("These are middleware functions that...") is very verbose and quite difficult to digest. I'm not too sure we need to talk much about the default mount path. Or perhaps something like having separate sentences e.g.

These are middleware functions that are executed in every incoming request matching the specified path. If you don't specify a path, the path defaults to / which will match every incoming request. As with any middleware functions, they will not run if the request-response cycle ends before reaching them.

Thoughts?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's probably just remove the sentence regarding the mount path, since I would assume this is more useful and commonly used for attaching. routers and will probably be discussed in the router lesson, what do you think?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense I think. Can always revisit this depending on exactly what the routes lesson goes through and how.

nodeJS/express/controllers.md Outdated Show resolved Hide resolved
nodeJS/express/controllers.md Outdated Show resolved Hide resolved
nodeJS/express/controllers.md Outdated Show resolved Hide resolved
nodeJS/express/controllers.md Outdated Show resolved Hide resolved
nodeJS/express/controllers.md Outdated Show resolved Hide resolved
nodeJS/express/controllers.md Outdated Show resolved Hide resolved
nodeJS/express/controllers.md Outdated Show resolved Hide resolved
nodeJS/express/controllers.md Outdated Show resolved Hide resolved
fcasibu and others added 3 commits April 4, 2024 19:14
Co-authored-by: MaoShizhong <122839503+MaoShizhong@users.noreply.github.com>
Co-authored-by: MaoShizhong <122839503+MaoShizhong@users.noreply.github.com>
@fcasibu
Copy link
Contributor Author

fcasibu commented Apr 5, 2024

Resolved all unresolved comments, let me know if there was a mistake. Thanks

fcasibu and others added 2 commits April 15, 2024 12:58
Co-authored-by: MaoShizhong <122839503+MaoShizhong@users.noreply.github.com>
Copy link
Member

@01zulfi 01zulfi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@fcasibu this is a chonky lesson 🔥

Particularly love the error handling section. Super cool that it goes through custom errors as well.

Just left a couple of comments below, allow me more time later to dig into the content and code examples.

nodeJS/express/controllers.md Outdated Show resolved Hide resolved
nodeJS/express/controllers.md Outdated Show resolved Hide resolved

A middleware function typically takes three parameters (however, there is one that we will get into later that has four):

**NOTE**: Names are just convention, you can name them whatever you want `req -> request, res -> response, etc`
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this note should be after the list that explains the parameters

@fcasibu fcasibu requested a review from 01zulfi April 28, 2024 08:33
- `res` - The response object, which represents the HTTP response that will be sent back to the client.
- `next` - The function that pass the control to the next middleware function in the chain (we'll get to this later). This is optional.

**NOTE**: Names are just convention, you can name them whatever you want `req -> request, res -> response, etc`
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we have this note in the lesson note styling?

nodeJS/express/controllers.md Outdated Show resolved Hide resolved

#### Router-level middleware

Router-level middleware works similarly to application-level middlewares but is bound to an *instance of Express router* using router.use or router.METHOD (e.g. router.get) functions. This however is only executed when the request matches the specific route as you've probably already learned in the Routes lesson.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we have syntax highlighting for router.use and router.METHOD?

nodeJS/express/controllers.md Outdated Show resolved Hide resolved
nodeJS/express/controllers.md Outdated Show resolved Hide resolved
res.send(`User found: ${user.name}`);
});
```

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we add a lesson note of tip variant here? It should say something along the lines:

the asyncHandler function in the express-async-handler function is just 6 lines of code. try to take a guess on how it's implemented and then do check out the source code for yourself.

Copy link
Contributor Author

@fcasibu fcasibu May 11, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should I remove It essentially just chains a catch to "catch" any errors in the function above and let them take a guess instead like described here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed it but let me know about your thoughts on that

Comment on lines 218 to 220
- `req` - The request object, which represents the incoming HTTP request.
- `res` - The response object, which represents the HTTP response that will be sent back to the client.
- `next` - The function that pass the control to the *next* middleware function (We'll get to this later). This is optional.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we don't need to explain these params again

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Condensed it to

It is the same as the previous middleware functions with three parameters but with one new parameter in a different order which is the error object. This is an odd one but the error object must be the first parameter in the callback.

nodeJS/express/controllers.md Outdated Show resolved Hide resolved
Comment on lines 388 to 397
router.route('/')
.get(userController.getUsers)
.post(userController.createUser);
// You will likely place your validation/authentication middleware functions here or perhaps in the controller file, e.g.
// .post(validationMiddleware, userController.createUser)

router.route('/:id')
.get(userController.getUserById);

module.exports = router;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we follow the router.get("route") and router.post("route") syntax, since that's what we use in other lessons.

Co-authored-by: 01zulfi <85733202+01zulfi@users.noreply.github.com>
@fcasibu fcasibu force-pushed the node-revamp/nodejs-controllers-lesson branch from cc01fa5 to 28e1566 Compare May 11, 2024 10:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Content: NodeJS Involves the NodeJS course Project Node Revamp Issues/PRs related to the Node Revamp project
Projects
None yet
Development

Successfully merging this pull request may close these issues.

New Lesson: Controllers
3 participants