Static image file is invisible in node.js/express

旧城冷巷雨未停 提交于 2019-12-11 12:40:48

问题


The following code is an exemple of code to serve static files in express on node.js. If I ask the server a .css file there is no problem, but if I ask an image such as a jpg or png, it loads a white page (no /GET error or anything, a simple white page). In my developer tool in my browser I see the following warning: 1Resource interpreted as Document but transferred with MIME type image/jpeg. How can I fix this?

I am using cloud9ide and express 2.4.6

var express = require("express"),
    app = express.createServer();

app.use(express.static(__dirname + '/static'));

app.get('/', function(req, res){
    res.send('Hello World');
});

app.listen(process.env.C9_PORT);

回答1:


It looks like the file in question is not in JPEG format. Can you save that URL as a file using wget, curl or similar tools and open that file in a text editor?

A valid JPEG file should look like binary garbage and should have "JFIF" signature string close to the beginning (byte offset 6 I think).

it is possible that the file contains an error message instead of valid JPEG data.




回答2:


It seems to be a bug from cloud9 ide. I tried my code locally and it worked. There is a ticket open on cloud9ide at: http://cloud9ide.lighthouseapp.com/projects/67519/tickets/390-get-png-image-not-working-in-preview-mode#ticket-390-4



来源:https://stackoverflow.com/questions/7506797/static-image-file-is-invisible-in-node-js-express

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!