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).
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).