javascript – Node.js – 文件系统获取文件类型

栏目: Node.js · 发布时间: 7年前

内容简介:看看http://stackoverflow.com/questions/10431845/node-js-file-system-get-file-type

我需要在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 中没有找到一个功能,所以如果有人能告诉我如何做,我会很高兴.

看看 mmmagic module .它是一个libmagic绑定,似乎完全符合你想要的.

http://stackoverflow.com/questions/10431845/node-js-file-system-get-file-type


以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Geometric Folding Algorithms

Geometric Folding Algorithms

Erik D Demaine / Cambridge University Press / 2008-8-21 / GBP 35.99

Did you know that any straight-line drawing on paper can be folded so that the complete drawing can be cut out with one straight scissors cut? That there is a planar linkage that can trace out any alg......一起来看看 《Geometric Folding Algorithms》 这本书的介绍吧!

RGB HSV 转换
RGB HSV 转换

RGB HSV 互转工具

HEX CMYK 转换工具
HEX CMYK 转换工具

HEX CMYK 互转工具

HSV CMYK 转换工具
HSV CMYK 转换工具

HSV CMYK互换工具