Defining mimetypes for express

Express, the node.js framework, uses mime to automatically find the mime-type of any statically served file.

If you have a text file with a certain extension and it’s being served as binary, you can serve it as text by defining its mime-type within mine.

For example, I had a .coffee file and I wanted express to serve it as a text file, so here’s what I did:

# let .coffee files be served as text
mime = require "mime"
mime.define({'text/coffeescript' : ['coffee']})

Basically you call mime.define and give it an object formatted like this:

{
     'mime-type' : ['list', 'of', 'extensions'],
     'mime-type' : ['list', 'of', 'extensions'],
     'mime-type' : ['list', 'of', 'extensions'],
}

For example:

{
     'text/plain' : ['cpp', 'c', 'd', 'java']
}

In other words, keys are mime-types, values are a list of extensions that should be served with that mime-type.

That’s all you need to do. The rest happens automatically.