在NodeJS中使用fs.read文件显示图像



我有一个简单的脚本,可以为自制框架加载css(我使用的不是express,而是http(我的功能如下:

function getStatic(req, res) {
console.log(req.params);
if (fs.existsSync('./public/' + req.params.file)) {
var file = fs.readFileSync('./public/' + req.params.file, 'utf8');

res.write(file);
res.end();
} else {
res.writeHead(200, {
"Content-Type": "text/html; charset=utf-8"
});
res.write("⚽⚽⚽⚽ 404 - not found ⚽⚽⚽⚽");
console.log(req);
res.end('error')
console.log(req.params);
}
}

不幸的是,图像显示为文件下载。我在nodejs中找到了类似的答案-如何读取和输出jpg图像?但我不确定如何在我的代码中集成它,以覆盖jpg、png和可能的其他图像类型?

您必须设置内容类型,以便浏览器能够正确处理请求的文件。

顺便说一句,有一个本地文件包含漏洞,你可以在那里了解更多信息:https://en.wikipedia.org/wiki/File_inclusion_vulnerability

相关内容

最新更新