Skip to content

Instantly share code, notes, and snippets.

@gnitnuj
Last active February 14, 2018 21:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gnitnuj/9318fbe9eae86a6e1f7e01b7eaa28cc6 to your computer and use it in GitHub Desktop.
Save gnitnuj/9318fbe9eae86a6e1f7e01b7eaa28cc6 to your computer and use it in GitHub Desktop.
module to get content type from file extension
// this file exists just to name the gist, and because gist ordering is asciibetical, it's name starts with a capital letter.
const path = require('path');
/**
* getContentType
* Given a filepath, getContentType returns the mime-type associated to the content
*
* @param {string} - filepath
*/
module.exports = (filePath) => {
switch (path.extname(filePath).toLowerCase()) {
case '.html':
return 'text/html';
case '.png':
return 'image/png';
case '.gif':
return 'image/gif';
case '.jpg':
case '.jpeg':
return 'image/jpeg';
case '.svg':
return 'image/svg+xml';
case '.css':
return 'text/css';
case '.js':
return 'text/javascript';
default:
return 'application/json';
}
};
{
"name": "content-type",
"version": "0.1.0"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment