Unable to run a Node.JS code example correctly

I'm reading Learning Node.js by Marc Wandschneider. I've been able to do everything in the chapters up until Chapter 6, where we get to serving static pages, using Mustache.JS

Granted, the book is 8 years old.

The chapter I'm working on is here

https://github.com/marcwan/LearningNodeJS/tree/master/Chapter07/02_basic_routing
and the error I'm working on I believe is generating from here:

https://github.com/marcwan/LearningNodeJS/blob/master/Chapter07/02_basic_routing/server.js
I believe it's coming from this portion:

function serve_static_file(file, res) {
var rs = fs.createReadStream(file);
rs.on(
'error',
function (e) {
res.writeHead(404, { "Content-Type" : "application/json" });
var out = { error: "not_found",
message: "'" + file + "' not found" };
res.end(JSON.stringify(out) + "\n");
return;
}
);

var ct = content_type_for_file(file);
res.writeHead(200, { "Content-Type" : ct });
rs.pipe(res);

}


Because there's no res.end, but I'm not sure, because I'm very knew at this. When I first run the program, I get an error like it's not recognizing the .jpg file I put in the folder.
I then went to http://localhost:8080/pages/home, it tries to load, and then I get an a "Cannot write headers after they are sent to the client"

and the first error beneath is that shows up is at ServerResponse.writeHead (node:_http_server:345:11).
 

Afty

Ars Scholae Palatinae
1,434
Two things:

I think the underlying problem is that, in an error condition, you are trying to write the headers twice, once in the callback on error, and again in the normal path. Note that the callback executes separately from the main path, and so the return there doesn't return from static_serve_file. I think if you write it like this, it might work:

Code:
function serve_static_file(file, res) {
    var rs = fs.createReadStream(file);
    rs.on('error', function (e) {
        res.writeHead(404, { "Content-Type": "application/json" });
        var out = { error: "not_found", message: "'" + file + "' not found" };
        res.end(JSON.stringify(out) + "\n");
    });

    rs.on('open', function () {
        var ct = content_type_for_file(file);
        res.writeHead(200, { "Content-Type": ct });
        rs.pipe(res);
    });
}

Second thing: ChatGPT is super helpful for debugging problems like this. I pasted in your post verbatim to ChatGPT and it found the mistake and fixed it. And it won't complain about the way you formatted your question.
 

MilleniX

Ars Tribunus Angusticlavius
8,138
Subscriptor++
Second thing: ChatGPT is super helpful for debugging problems like this. I pasted in your post verbatim to ChatGPT and it found the mistake and fixed it. And it won't complain about the way you formatted your question.
It would be really funny if a code-oriented LLM trained heavily on StackOverflow learned to hassle people about all the things that the moderation rules and culture there impose. "This looks like a homework question" "You're asking for opinions"
 
  • Haha
Reactions: Aleamapper
Ever since I learned a tiny bit about server side web programming, I always wonder how ChatGPT obtained the source code of programs that the server is supposed to run, not serve.

I guess this is fine.:)
The same way the rest of us do.

Git hub.
Text books.
API documentation.

It's like a junior developer with an extremely wide field of interest.