Closed skiadas closed 10 years ago
I am getting the error: "can't set headers after they are sent." whenever I try and send a get request to the url: localhost:8000/readLogFile . It is saying that their is a problem with line 24, which reads: res.status(200).send(data);
Have a look at the documentation for res.send and res.end. I don't think you can use them both:
http://expressjs.com/4x/api.html#res.end
Basically once you do res.send, it's over, you cannot mess with res any more, the reply has been sent.
I forgot to remove that, but that isn't the problem. I added that in after I had originally gotten the error hoping that it would be the solution to the problem.
Hm ok, which file should I be looking at? I thought it was https://github.com/jaseew01/multi-user-file-manager/blob/master/beginningWebServers.js but that is filled with res.end
that follow res.send
That is the right file, take a look at what I just committed, I am saying that even when I take out res.end(), I still get the same error.
Okay I think I see the problem. So you have this request. You have two options. Option 1 is to do some work on it, change some things, then call next()
and have someone else handle it. Option 2 is to use res.send
or res.end
to handle the request yourself and send a reply. But you can't do both. So your calls to next probably made some requests get handled by multiple methods of yours, and that means multiple res.send
. Each of your handlers needs to decide if it is the one responsible for responding, or whether it should delegate that task.
Got it.
Learn the basics of Express (http://expressjs.com/4x/api.html#application and the node book) and create a rudimentary server: