Skip to content

Unable to serve static png image #21

Description

@ZippCodder

This course is amazing so far and ive learned a very substancial amount, however i have came across one issue that i cannot seen to figure out. Im trying to serve static files like css, js, and a favicon image, but when i try and serve a .png image, it just shows up as a black square, or if referenced in the html dosent show up at all. I sent the image by doing the following:

const http = require("http");
const fs = require("fs");
const server = http.createServer((req,res) => {
fs.readFile("imagePath",(err,imgData) => {
res.setHeader("Content-Type","image/png");
res.end(imgData);
})
});
server.listen(3000);

The path is correct, no error occurs while reading the png file, and i even logged out "err" and "imgData", and they are indeed wht they should be , which is "null" and a buffer. But in the browser it dosent seem to have rendered properly. Dose anyone have a work around or know somthing that im missing?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions