Home>Article>Web Front-end> What type of file does node read?

What type of file does node read?

藏色散人
藏色散人 Original
2022-12-29 14:35:12 2166browse

Node method of reading file type: 1. Through "res.writeHead(200, {'Content-type': mime.getType(`public/a.webp`)});res.write( chunk);" method to pass in the file path to get the "mime-type"; 2. Get the file type through the file fileTypeFromFile or Buffer stream.

What type of file does node read?

#The operating environment of this tutorial: Windows 10 system, node v10.16.0 version, Dell G3 computer.

What type of file does node read?

Node.js gets the file type of the file

When using Node for file processing, we often need different types of files for different processing, and Return the corresponding request header to the client. Two plug-ins are recommended here to quickly obtain the file type.

1.mime

You can get the mime-type of the file for request header return

res.writeHead(200, { 'Content-type': mime.getType(`public/a.webp`)// image/webp }); res.write(chunk);

You only need to introduce the plug-in, and then use the corresponding function to pass in the file path. Get mime-type

2.file-type

Full-featured, you can type files through files or Buffer streams, and you can not only get mime-type but also file suffix types

import {fileTypeFromFile} from 'file-type'; console.log(await fileTypeFromFile('Unicorn.png')); //=> {ext: 'png', mime: 'image/png'}

And the usage method is given on the official website, which also obtains the type by passing the file path or Buffer. It also supports Promise, and the weekly download volume of this plug-in is tens of millions (2022-05-29)

What type of file does node read?

Recommended learning: "node.js video tutorial"

The above is the detailed content of What type of file does node read?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn