Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Express and Node.js, is it possible to extend or override methods of the response object?

Tags:

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

like image 308
M.K. Safi Avatar asked Aug 18 '13 22:08

M.K. Safi


People also ask

What is method override in Nodejs?

Lets you use HTTP verbs such as PUT or DELETE in places where the client doesn't support it.

What is the response object in Express?

The response object specifies the HTTP response when an Express app gets an HTTP request. The response is sent back to the client browser and allows you to set new cookies value that will write to the client browser.

What is the difference between Express and next js?

Express. js is a backend framework for building APIs whereas Next. js is a full stack framework that builds upon React to improve the SEO, development experience, and performance of your project. One of these features IS a built-in API routing system that could replace Express.

Which method sends response data in Express?

append() Methods. The . send() method on the res object forwards any data passed as an argument to the client-side. The method can take a string, array, and an object as an argument.


1 Answers

The best idea would be to add a custom method to the prototype of the response object:

var express = require("express");  express.response.customRender = function() {     // your stuff goes here }; 

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

var res = module.exports = {   __proto__: http.ServerResponse.prototype }; 

And this object becomes a prototype of newely created response object (which comes from connect framework):

res.__proto__ = app.response; 

(app.response is just an alias to res 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 with Object.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

like image 82
freakish Avatar answered Oct 29 '22 00:10

freakish