Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/39.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Html 尝试使用内容类型image/jpeg响应时图像损坏_Html_Node.js_Image_Express_Routing - Fatal编程技术网

Html 尝试使用内容类型image/jpeg响应时图像损坏

Html 尝试使用内容类型image/jpeg响应时图像损坏,html,node.js,image,express,routing,Html,Node.js,Image,Express,Routing,在我的前端html中,我需要一个带有源“/uploads/profilePicture”的img标记。在node/express中处理请求时,我希望对用户进行身份验证,从文件系统加载用户的配置文件图片,并使用图像数据进行响应。我的代码基本上可以归结为: app.get('*', function(req, res, next) { /* Some code leading to the function getProfilePicture */ }); function getProf

在我的前端html中,我需要一个带有源“/uploads/profilePicture”的
img
标记。在node/express中处理请求时,我希望对用户进行身份验证,从文件系统加载用户的配置文件图片,并使用图像数据进行响应。我的代码基本上可以归结为:

app.get('*', function(req, res, next) {
    /* Some code leading to the function getProfilePicture */
});

function getProfilePicture(req, res, next) {
    loadFile('uploads/' + req.user.local.picture, function(image) {
        res.writeHead(200, { 'Content-Type': 'image/jpeg' });
        res.end(image);
    });
};
运行代码时,前端会显示一个中断的图像,但控制台中不会打印任何错误

有趣的是,这以前对我很有效。不久前,我的路由代码没有那么通用,因为它对我网站上的每个页面都使用了
app.get()
。所以我使用了
app.get('/uploads/profilePicture')
而不是
app.get('*')
。这可能是问题所在吗

编辑:

我正在使用节点v0.10.35和express 3.0.6

编辑2:

使用Express 3.19.1进行了测试,但仍然不起作用。

结果表明,使用确实起到了作用

res.status(200).sendfile(URL);
请解释为什么sendfile可以工作,而不是setHeaders+end,我们将不胜感激