How to Get File MIME Type in Node.js?

24-Sep-2022

.

Admin

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

I hope it can help you...

#Node JS