首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >Node.js -文件系统获取文件类型,2012年左右的解决方案

Node.js -文件系统获取文件类型,2012年左右的解决方案
EN

Stack Overflow用户
提问于 2012-05-03 20:55:50
回答 4查看 63.5K关注 0票数 27

我需要在node.js的帮助下获取文件的文件类型,以设置内容类型。我知道我可以很容易地检查文件扩展名,但我也有文件没有扩展名,它的内容类型应该是image/pngtext/html aso。

这是我的代码(我知道它没有多大意义,但这是我需要的基础):

代码语言:javascript
复制
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中找到一个这样的函数,所以如果有人能告诉我怎么做,我会很高兴的。

EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/10431845

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档