This tutorial will explain the steps to determine the Mime Type or commonly called as Media Type of any file in NodeJS
What is a Mime Type?
The MIME type usually has two parts type and subtype. Eg: image/jpg, image/jpeg, video/mp4.
As per the definition taken from Mozilla, A media type (also known as a Multipurpose Internet Mail Extensions or MIME type) indicates the nature and format of a document, file, or assortment of bytes. MIME types are defined and standardized in IETF’s RFC 6838.
Reference from Mozilla – https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/MIME_types
The complete list of Media Types can be reference from the link https://www.iana.org/assignments/media-types/media-types.xhtml
Install the npm mime library
npm install mime
Reference the mime library in the NodeJS file
const mime = require("mime");
const mimeType = mime.getType(filePath);
As a bonus, the filePath passed to the method must be an actual file path which can constructed using the path Node libray
const path = require("path");
let fileUrl = "/uploadedFile.jpg"
let filePath = path.normalize(path.join(__dirname, "../tempPath/", fileUrl));