我需要使用Node.js来获取文件的文件类型,以便设置内容类型。我知道可以轻松地检查文件扩展名,但我也有没有扩展名的文件,它们应该具有内容类型
这是我的代码(我知道它没有太多意义,但这是我需要的基础):
我在API中没有找到相应的功能,那么我该如何做呢?
image/png
或text/html
。这是我的代码(我知道它没有太多意义,但这是我需要的基础):
var http = require("http"),
fs = require("fs");
http.createServer(function(req, res) {
var data = "";
try {
/*
* Do not use this code!
* It's not async and it has a security issue.
* The code style is also bad.
*/
data = fs.readFileSync("/home/path/to/folder" + req.url);
var type = "???"; // how to get the file type??
res.writeHead(200, {"Content-Type": type});
} catch(e) {
data = "404 Not Found";
res.writeHead(404, {"Content-Type": "text/plain"});
}
res.write(data);
res.end();
}).listen(7000);
我在API中没有找到相应的功能,那么我该如何做呢?
readFileSync
?听起来不是一个好主意... - ThiefMaster