jaseew01 / multi-user-file-manager

0 stars 0 forks source link

Starting on Web servers #10

Closed skiadas closed 10 years ago

skiadas commented 10 years ago

Learn the basics of Express (http://expressjs.com/4x/api.html#application and the node book) and create a rudimentary server:

jaseew01 commented 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);

skiadas commented 10 years ago

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.

jaseew01 commented 10 years ago

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.

skiadas commented 10 years ago

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

jaseew01 commented 10 years ago

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.

skiadas commented 10 years ago

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.

jaseew01 commented 10 years ago

Got it.