Node.js - 文件系统获取文件类型

时间:2021-08-22 13:24:16

I need to get the file type of a file with the help of node.js to set the content type. I know I can easily check the file extension but I've also got files without extension which should have the content type image/png, text/html aso.

我需要在node.js的帮助下获取文件的文件类型来设置内容类型。我知道我可以轻松检查文件扩展名,但我也有没有扩展名的文件应该有内容类型image / png,text / html aso。

This is my code (I know it doesn't make much sense but that's the base I need):

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

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);

I haven't found a function for that in the API so I would be happy if anyone can tell me how to do it.

我还没有在API中找到这个功能,所以如果有人能告诉我怎么做,我会很高兴。

5 个解决方案

#1


17  

Have a look at the mmmagic module. It is a libmagic binding and seems to do exactly what you want.

看看mmmagic模块。它是一种libmagic绑定,似乎完全符合你的要求。

#2


26  

There is a helper library for looking up mime types https://github.com/broofa/node-mime

有一个帮助库,用于查找mime类型https://github.com/broofa/node-mime

var mime = require('mime');

mime.lookup('/path/to/file.txt');         // => 'text/plain'

But it still uses the extension for lookup though

但它仍然使用扩展名进行查找

#3


10  

You should have a look at the command line tool file (Linux). It attempts to guess the filetype based on the first couple of bytes of the file. You can use child_process.spawn to run it from within node.

您应该查看命令行工具文件(Linux)。它试图根据文件的前几个字节猜测文件类型。您可以使用child_process.spawn从节点内运行它。

#4


6  

You want to be looking up the mime type, and thankfully node has a handy library just for that:

你想要查找mime类型,谢天谢地,node有一个方便的库:

https://github.com/bentomas/node-mime#readme

https://github.com/bentomas/node-mime#readme

edit:

编辑:

You should probably look into a static asset server and not be setting any of this stuff yourself. You can use express to do this really easily, or there's a whole host of static file modules, e.g. ecstatic. On the other hand you should probably use nginx to serve static files anyway.

您应该查看静态资产服务器,而不是自己设置任何这些内容。你可以使用express来轻松地完成这项工作,或者有一大堆静态文件模块,例如:欣喜若狂。另一方面,您应该使用nginx来提供静态文件。

#5


1  

I used this:

我用过这个:

npm install mime-types

And, inside the code:

而且,在代码中:

var mime = require('mime-types');
tmpImg.contentType = mime.lookup(fileImageTmp);

Where fileImageTmp is the copy of image stored on the file system (in this case tmp).

其中fileImageTmp是存储在文件系统上的图像副本(在本例中为tmp)。

The result I can see is: image/jpeg

我能看到的结果是:image / jpeg

#1


17  

Have a look at the mmmagic module. It is a libmagic binding and seems to do exactly what you want.

看看mmmagic模块。它是一种libmagic绑定,似乎完全符合你的要求。

#2


26  

There is a helper library for looking up mime types https://github.com/broofa/node-mime

有一个帮助库,用于查找mime类型https://github.com/broofa/node-mime

var mime = require('mime');

mime.lookup('/path/to/file.txt');         // => 'text/plain'

But it still uses the extension for lookup though

但它仍然使用扩展名进行查找

#3


10  

You should have a look at the command line tool file (Linux). It attempts to guess the filetype based on the first couple of bytes of the file. You can use child_process.spawn to run it from within node.

您应该查看命令行工具文件(Linux)。它试图根据文件的前几个字节猜测文件类型。您可以使用child_process.spawn从节点内运行它。

#4


6  

You want to be looking up the mime type, and thankfully node has a handy library just for that:

你想要查找mime类型,谢天谢地,node有一个方便的库:

https://github.com/bentomas/node-mime#readme

https://github.com/bentomas/node-mime#readme

edit:

编辑:

You should probably look into a static asset server and not be setting any of this stuff yourself. You can use express to do this really easily, or there's a whole host of static file modules, e.g. ecstatic. On the other hand you should probably use nginx to serve static files anyway.

您应该查看静态资产服务器,而不是自己设置任何这些内容。你可以使用express来轻松地完成这项工作,或者有一大堆静态文件模块,例如:欣喜若狂。另一方面,您应该使用nginx来提供静态文件。

#5


1  

I used this:

我用过这个:

npm install mime-types

And, inside the code:

而且,在代码中:

var mime = require('mime-types');
tmpImg.contentType = mime.lookup(fileImageTmp);

Where fileImageTmp is the copy of image stored on the file system (in this case tmp).

其中fileImageTmp是存储在文件系统上的图像副本(在本例中为tmp)。

The result I can see is: image/jpeg

我能看到的结果是:image / jpeg