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);
APIでそのための関数を見つけられなかったので、誰かがそれを行う方法を教えてくれたら嬉しいです。