So, I am making a website with Node.js, and am using an http server. I am trying to serve an image with it. The image is definitely shown, but only the very top part of it.
Here is the image, what happens, and the server code (in that order).
const server = http.createServer((req, res) => {
// get the file path from req.url, or '/public/index.html' if req.url is '/'
const filePath = ( req.url === '/' ) ? '/public/index.html' : req.url;
// determine the contentType by the file extension
const extname = path.extname(filePath);
let contentType = 'text/html';
if (extname === '.js') contentType = 'text/javascript';
else if (extname === '.css') contentType = 'text/css';
else if(extname === (".png")) {
var imagePath = path.join(__dirname, '/', req.url);
var fileStream = fs.createReadStream(imagePath);
res.writeHead(200, {"Content-Type": "image/png"});
fileStream.pipe(res);
}
// pipe the proper file to the res object
res.writeHead(200, { 'Content-Type': contentType });
fs.createReadStream(`${__dirname}/${filePath}`, 'utf8').pipe(res);
});
Also, I am using websocket, if that helps.
