question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

TypeError: res.writeHead is not a function

See original GitHub issue

Hi, I’ve done this with your latest code:

npm run build npm run start

Right after the Open http://localhost:8080 in a browser to view the app. message, I get an exception:

[0] /Users/me/projects/react-redux-universal-hot-example/src/server.js:105
[0]     res.writeHead(500, { 'content-type': 'application/json' });
[0]         ^
[0] 
[0] TypeError: res.writeHead is not a function
[0]     at ProxyServer.<anonymous> (/Users/me/projects/react-redux-universal-hot-example/src/server.js:55:9)

This is the code that fails:

// added the error handling to avoid https://github.com/nodejitsu/node-http-proxy/issues/527
proxy.on('error', (error, req, res) => {
  let json;
  if (error.code !== 'ECONNRESET') {
    console.error('proxy error', error);
  }
  if (!res.headersSent) {
    res.writeHead(500, {'content-type': 'application/json'});   // <- EXCEPTION
  }

I installed the latest http-proxy package but nothing changed.

Thanks, Alvaro

Issue Analytics

  • State:open
  • Created 8 years ago
  • Comments:16

github_iconTop GitHub Comments

4reactions
realbuggercommented, Apr 1, 2016

@stalkingbutler I also run into this problem whenever I change the server side code. Instead of removing the line all together, I added a check to see if res.writeHead is a function. So far so good.

if (!res.headersSent) {
  if (typeof res.writeHead === 'function') res.writeHead(500, {'content-type': 'application/json'});
}
1reaction
tearsofphoenixcommented, Feb 21, 2016

I know WHY!!! You must have run some other NodeJS App which use app cache on port 3000, and your browser must be Chrome! It’s ok when use Safari!!! @alvaro1728

Read more comments on GitHub >

github_iconTop Results From Across the Web

TypeError: res.writeHead is not a function chapter 6 Fullstack ...
I have used the request and response in the http.createServer() in the wrong order, I used response then request, should be request and...
Read more >
Error: response.write is not a function (Example) - Treehouse
Hey all, I am doing the exercise Build a Simple Dynamic Site with Node.js >> Reading from Files (7:30). When I try to...
Read more >
Randomly getting TypeError: res.writeHead is not a function at ...
Hi, out of nowhere I'm getting this weird error on localhost running netlify dev… it's happening randomly, works for some time, ...
Read more >
Fix errorMiddleware to prevent "TypeError: res.writeHead is ...
Fix errorMiddleware to prevent "TypeError: res.writeHead is not a function" (#3913. An error occurred. ) ... const errorMiddleware = (err, req, res) =>...
Read more >
Node.js response.writeHead() Method - GeeksforGeeks
The response.writeHead() (Added in v1..0) property is an inbuilt property of the 'http' module which sends a response header to the request.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found