javascript – Node.js – 檔案系統獲取檔案型別
我需要在node.js的幫助下獲得一個檔案的檔案型別來設定內容型別.我知道我可以很容易地檢查副檔名,但是我也有沒有副檔名的檔案應該有內容型別image / png,text / html aso.
這是我的程式碼(我知道它沒有什麼意義,但這是我需要的基礎):
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);
我在ofollow,noindex" target="_blank">API 中沒有找到一個功能,所以如果有人能告訴我如何做,我會很高興.
看看mmmagic module .它是一個libmagic繫結,似乎完全符合你想要的.
http://stackoverflow.com/questions/10431845/node-js-file-system-get-file-type