【发布时间】:2020-06-20 13:14:33
【问题描述】:
我有一个为自制框架加载 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 - How to read and output jpg image?,但我不确定如何将它集成到我的代码中以涵盖 jpg、png 和可能的其他图像类型?
【问题讨论】:
-
我认为这不是确切的方法,您仍然可以使用代码动态获取内容类型
标签: javascript node.js