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
Post a Comment