node.js - res.writeHead override not working -


i'm trying override res.writehead method in node.js, though it's throwing error. here's code:

const http = require('http'); http.createserver((req, res) => {     const _writehead = res.writehead;     res.writehead = (...a) => {         console.log('res.writehead called!');         _writehead(...a);     };     res.writehead(200, {         'content-type': 'text/plain'     });     res.end('hello, world!'); }).listen(2020); 

res.writehead called! logged , typeerror: cannot read property 'statusmessage' of undefined when client connects. why?

when calling _writehead, this object not refer res. refers global context instead, preventing node.js functioning properly. change const _writehead = res.writehead; const _writehead = res.writehead.bind(res);.


Comments

Popular posts from this blog

Retrieving ETA (estimated time of arrival) with Google Distance Matrix API and public transit as transport mode -

javascript - Confirm a form & display message if form is valid with JQuery -

ionic framework - Meteor - Error: Failed to execute 'insertBefore' on 'Node' -