With every middleware, Express passes a res
and a req
objects. These objects extend the native ones that come from http.ServerResponse
and http.ClientRequest
respectively. I'd like to know if it's possible to override or extend methods of the response object.
For example, instead of res.render('home', jsonData);
, I'd like to extend res
with a custom method called customRender and use it like so: res.customRender()
.
I'm not stuck at a particular problem or anything. I'd just like to learn how to extend native objects or, as with this case, object that come from 3rd party modules in Node.js
As freakish answered, you can add a method to the prototype. However, it won't have access to other members of the created object. If you want to have that access, you need to go a step further. This is a full example:
This way, you can create your express objects like this:
From the response objects, you'll be able to call the
message
anddata
functions directly.The best idea would be to add a custom method to the prototype of the response object:
And this function should be accessible by every
res
object.You can read the source code to see how they extend native objects. Basically they are doing prototype chaining:
express/lib/response.js
And this object becomes a prototype of newely created response object (which comes from connect framework):
(
app.response
is just an alias tores
defined above). Note that__proto__
property is a reference to the prototype of an object.Be warned though. First of all
__proto__
is not a part of EcmaScript (it might not be available in other JavaScript implementations). Secondly: normally you would do inheritance withObject.create
(setting__proto__
directly on an object is a monkey patching and it is generally a bad practice, it may break many things). Read more about that here:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Inheritance_and_the_prototype_chain
Just add a middleware that adds the customRender function to res.