Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to serve static png image #21

Open
ZippCodder opened this issue Nov 11, 2020 · 1 comment
Open

Unable to serve static png image #21

ZippCodder opened this issue Nov 11, 2020 · 1 comment

Comments

@ZippCodder
Copy link

ZippCodder commented Nov 11, 2020

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?

@spriggs81
Copy link

So if I were you, the first place I would be looking for is in my browser's dev tools. Under the source and pages (in chrome), I would make sure that the png file is really being received on the client-side. If you have an error there, a screenshot will help someone with providing more advice if you're not able to understand the problem yourself. If you don't see the file, then the file isn't being received on the client-side, so it's more than likely being sent incorrectly on the server-side.

If that's not the problem, more source code access will help in my case.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants