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

c# - Update a combobox from a presenter (MVP) -

How to understand 2 main() functions after using uftrace to profile the C++ program? -

How to put a lock and transaction on table using spring 4 or above using jdbcTemplate and annotations like @Transactional? -