-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
Guide to overriding sugar methods #763
Conversation
I may have some small wording improvements, but I'll wait first to see if there are any technical review comments. |
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.
One technical-level comment I had :)
9105d0b
to
0aebe2a
Compare
Now that I read this more closely, I have a couple of more substantive questions/comments...
This begs the question of exactly which methods are sugar methods that you can override? Is it actually all of them? Only a subset? ...if so, we should clearly list them. Are there any that you shouldn't (even if you technically could?) Are there any side-effects of doing so to caveat? Rather than focus on what is technically possible, I think we should state "best practices" to the extent we know. Also, although I think most developers are familiar with the concept of "syntactic sugar" I don't think the term "sugar methods" would be universally understood. I suggest instead the title should just be "Overriding Express methods" and then explain the in the text what a "sugar method" is and (per above) exactly which methods they are. Also, it would be nice to give some idea of why you would want to do this, i.e. what you can accomplish by doing so. |
The Express request and response objects extend the Node HTTP request object and response object respectively, and add methods such as Technically, one can override all of the Express methods. They can even override Node HTTP objects' methods. Overriding Express or Node methods has a very open-ended outcome, so it is not possible to state "best practices" or side-effects. It can range from "same behavior" to "completely broken" and everything possible in between. Basically, if someone decides to override the methods, they are on their own, and should know what they are doing. One tip that should be given to the "overriders" is to look the source code of the methods, if they decide to override. That will give them a clear picture of what (else) is happening in the method.
Yes, "Overriding Express methods" sounds better. Let's change it as suggested.
Reason, "because Express does not do it the way I want it". Eg: #757 @crandmck I hope you will recompose the content based on my input. |
My PR was based on the discussion at expressjs/express#3151. After listening to the discussion in the recording, I think it makes sense to extend "sugar properties" as well. So the title would be now "Overriding the Express API" or something along that line. @dougwilson @crandmck I will add info about overriding properties and other possibilities in the next comment. |
Documented properties in the Express API (limited to A. Inherited from the Node core
There are many other core properties, only B. Assigned properties
These properties are assigned on to the C. Defined as getters.
Since A and B are dynamically added by the router for each new request-response cycle, they cannot be overriden. Properties under C are basically helper methods defined as getters, which return the values in the context of the current request-response cycle, so their behavior can be changed by re-assigning a new getter. The following code rewrites how the value of the
The assignment operator has no affect on properties defined with getters. For example:
|
@expressjs/express-tc do you suggest documenting the ability to extend and override the methods and properties on the express, app, and router instances as well? |
@hacksparrow Let's try to land this! In TC, we discussed just adding minimal info to start, and then we can add more later if we want to. Can you please make any final changes and then request final review? |
0aebe2a
to
bda0763
Compare
@crandmck I have updated the PR for final review. @expressjs/express-tc |
044e5df
to
c40ce1a
Compare
bda0763
to
104a8d8
Compare
939500c
to
7bc9163
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.
time for this to land. there was an issue recently where this was used, so good to have something to point to. rebased and fixed up for merge
Guide explaining sugar methods and how to override them; resolves #757.
@dougwilson @crandmck @raijinsetsu