My chrome browser is telling me that it has the test.html file but it is still just a blank page.
This is the html code that I am trying to send
and here is my javascript code that creates the server and the method that send the file to the client
sendFile(fileName){
const publicRoot = __dirname.slice(0,-4) + '/public';
const filePath = publicRoot + fileName;
//console.log(filePath);
let fileType = '';
for(let i=0; i<fileName.length; i++){
if(fileName[i] === "."){
fileType += fileName.slice(i+1).trim();
}
}
let encoding;
if(this.types[fileType].split('/')[0] === "text"){
encoding = 'utf8';
}else if(this.types[fileType].split('/')[0] === 'image'){
encoding = null;
}
//console.log(filePath);
fs.readFile(filePath, encoding, (err,data) => {
if(err){
//console.log(err);
this.setHeader('Content-Type', 'text/plain');
this.send(500, 'Internal Server Error');
throw err;
}else{
//console.log(this.types[fileType]);
this.setHeader('Content-Type', this.types[fileType]);
console.log(encoding);
console.log(this.headers);
this.writeHead(200);
console.log(data);
console.log(this);
this.write(data);
this.end();
}
});
}
}
const server = net.createServer((sock)=>{
sock.on('data', (binaryData) => {
//console.log(binaryData+'');
//creates a request Object with the constructor
const req = new Request(binaryData+'');
const res = new Response(sock);
//console.log(req.path);
if(req.path === "/"){
res.setHeader('Content-Type','text/html');
res.send(200,'<link rel = "stylesheet" type = "text/css" href = "foo.css"> <h2>this is a red header</h2><em>Hello</em> <strong>World</strong>');
}else if(req.path === "/foo.css"){
res.setHeader('Content-Type', 'text/css');
res.send(200, 'h2 {color: red;}');
}else if(req.path === "/test"){
res.sendFile("/html/test.html");
}else if(req.path === "/bmo1.gif"){
res.sendFile("/img/bmo1.gif");
}else{
res.setHeader('Content-Type', 'text/plain');
res.send(404, 'uh oh... 404 page not found!');
}
});
});
server.listen(PORT, HOST);
I've tried everything and looked up many other cases where somehow the html page does not load. No idea what could be wrong.
Aucun commentaire:
Enregistrer un commentaire