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


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

查看所有标签

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

Introduction to Programming in Java

Introduction to Programming in Java

Robert Sedgewick、Kevin Wayne / Addison-Wesley / 2007-7-27 / USD 89.00

By emphasizing the application of computer programming not only in success stories in the software industry but also in familiar scenarios in physical and biological science, engineering, and appli......一起来看看 《Introduction to Programming in Java》 这本书的介绍吧!

随机密码生成器
随机密码生成器

多种字符组合密码

HTML 编码/解码
HTML 编码/解码

HTML 编码/解码

正则表达式在线测试
正则表达式在线测试

正则表达式在线测试