How to Get File MIME Type in Node.js?
Hello Friends,
This simple article demonstrates how to get file mime type in node.js. if you want to see an example of get file mime type in node.js then you are in the right place. This article will give you a simple example of get file mime type in nodejs. This article will give you a simple example of get file mime type in node js.
In this example is get file mime type in nodejs. i will get file mime type mime-types package. This library is powered by mime-db data. It provides a very simple, lightweight, safe yet speed utility to deal with MIME types and file extensions.
So, let's start following example with output:
Step 1: Install Node JS
This step is not required; however, if you have not created the node js app, then you may go ahead and execute the below command:
mkdir my-app
cd my-app
npm init
Step 2: Install mime-types npm
In this step install mime-types package. This is use to get file mime type.
npm install mime-types
Step 3: Update server.js file
server.js
// import package mime-types
var mime = require('mime-types')
// get image mime type
var mimeType = mime.lookup('file.png')
// print image mime type
console.log(mimeType)
Output:
image/png
- How to Install Node Js and NPM Ubuntu?
- How to used Google ReCaptcha V3 in Node Js Express?
- How to Send Email with Attachment in Node js?
- How to Insert Data from Form into MySQL Database using Node js Express?
- Install Node Js and NPM Ubuntu 18.04/20.04/22.04 Tutorial
- How to Delete Data by id into MongoDB in Node JS?
- Install Node.js 16/17/18 on CentOS 8 Tutorial
- How to Upload File in MongoDB using Node js Express?