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

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

内容简介:看看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


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

查看所有标签

猜你喜欢:

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

零基础学算法

零基础学算法

戴艳 / 机械工业出版社 / 2012-3-1 / 69.00元

零基础学算法(第2版),ISBN:9787111372493,作者:戴艳 等编著一起来看看 《零基础学算法》 这本书的介绍吧!

JS 压缩/解压工具
JS 压缩/解压工具

在线压缩/解压 JS 代码

Markdown 在线编辑器
Markdown 在线编辑器

Markdown 在线编辑器