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
base: main
Are you sure you want to change the base?
Node revamp: Controllers Lesson #27695
Conversation
4bd9aab
to
f17bd6d
Compare
f17bd6d
to
0b67561
Compare
bec9a1a
to
f449a83
Compare
0bd8e04
to
0cc9f09
Compare
0cc9f09
to
bc5e301
Compare
There was a problem hiding this 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.
Co-authored-by: MaoShizhong <122839503+MaoShizhong@users.noreply.github.com>
776aaea
to
48196ad
Compare
Thanks @MaoShizhong I appreciate it! |
There was a problem hiding this 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
|
||
#### 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: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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?
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
Co-authored-by: MaoShizhong <122839503+MaoShizhong@users.noreply.github.com>
Co-authored-by: MaoShizhong <122839503+MaoShizhong@users.noreply.github.com>
Resolved all unresolved comments, let me know if there was a mistake. Thanks |
Co-authored-by: MaoShizhong <122839503+MaoShizhong@users.noreply.github.com>
There was a problem hiding this 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
|
||
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` |
There was a problem hiding this comment.
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
Co-authored-by: 01zulfi <85733202+01zulfi@users.noreply.github.com>
nodeJS/express/controllers.md
Outdated
- `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` |
There was a problem hiding this comment.
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
|
||
#### 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. |
There was a problem hiding this comment.
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
?
res.send(`User found: ${user.name}`); | ||
}); | ||
``` | ||
|
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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
nodeJS/express/controllers.md
Outdated
- `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. |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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
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; |
There was a problem hiding this comment.
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>
cc01fa5
to
28e1566
Compare
Because
To add a new lesson about Controllers in the MVC pattern for the node revamp
This PR
nodeJS/express/controllers.md
Issue
Closes #27615
Additional Information
Pull Request Requirements
location of change: brief description of change
format, e.g.Intro to HTML and CSS lesson: Fix link text
Because
section summarizes the reason for this PRThis PR
section has a bullet point list describing the changes in this PRIssue
section