Comparing version 0.1.1 to 0.1.2
30
index.js
'use strict' | ||
var mime = require('mime') | ||
var ext = require('path').extname | ||
var merge = require('lodash.merge') | ||
@@ -10,2 +11,3 @@ | ||
defaultType: 'text/html', | ||
defaultCharset: 'utf-8', | ||
charset: true | ||
@@ -18,4 +20,5 @@ } | ||
return function (req, res, next) { | ||
if (shouldDefineHeader(req, res)) { | ||
defineContentHeader(req, res) | ||
var path = req.path | ||
if (shouldDefineHeader(path, res)) { | ||
res.setHeader('Content-Type', getContentType(path, res)) | ||
} | ||
@@ -25,7 +28,15 @@ next() | ||
function defineContentHeader(req, res) { | ||
var type = mime.lookup(req.path, options.defaultType) | ||
res.setHeader('Content-Type', type + getCharset(type)) | ||
function getContentType(path, res) { | ||
if (ext(path).length > 0) { | ||
return lookupContentHeader(path) | ||
} else { | ||
return getDefaultType() | ||
} | ||
} | ||
function lookupContentHeader(path) { | ||
var type = mime.lookup(path, options.defaultType) | ||
return type + getCharset(type) | ||
} | ||
function getCharset(type) { | ||
@@ -36,5 +47,5 @@ var charset = mime.charsets.lookup(type) | ||
function shouldDefineHeader(req, res) { | ||
function shouldDefineHeader(path, res) { | ||
return res.getHeader('content-type') == null | ||
&& Array.isArray(options.ignore) ? shouldIgnore(req.path) : true | ||
&& Array.isArray(options.ignore) ? shouldIgnore(path) : true | ||
} | ||
@@ -48,3 +59,8 @@ | ||
} | ||
function getDefaultType() { | ||
return options.defaultType + | ||
(options.charset ? '; charset=' + options.defaultCharset : '') | ||
} | ||
} | ||
{ | ||
"name": "mimeware", | ||
"description": "Cross-framework HTTP middleware to infer the proper MIME content type response header", | ||
"version": "0.1.1", | ||
"description": "Framework agnostic HTTP middleware to infer the proper MIME content type response header", | ||
"version": "0.1.2", | ||
"license": "MIT", | ||
@@ -22,3 +22,4 @@ "repository": "h2non/mimeware", | ||
"engines": { | ||
"node": ">= 0.10.0" | ||
"node": ">= 0.10.0", | ||
"io": ">=1.0.0" | ||
}, | ||
@@ -25,0 +26,0 @@ "scripts": { |
# mimeware [][travis] [][gemnasium] [][npm] | ||
Node.js HTTP server middleware to infer and define the proper MIME content type as response header | ||
Node.js/io.js HTTP server middleware to infer and define the proper MIME content type as response header | ||
It works in multiple web frameworks such as Connect, Express, Restify, Sails... | ||
It is web framework agnostic and works properly in Connect, Express, Restify, Sails... among others | ||
which implements the standard connect-based middleware interface: `use(function (req, res, next) { ... })` | ||
Includes all 600+ types and 800+ extensions defined by the Apache project, plus additional types submitted by the node.js community. Uses [node-mime](https://github.com/broofa/node-mime) | ||
It includes all 600+ types and 800+ extensions defined by the Apache project, plus additional types submitted by the node.js community. It uses [node-mime](https://github.com/broofa/node-mime) | ||
It was implemented specially for older Express/Connect versions | ||
It was implemented specially for older Express/Connect versions | ||
which has no smart support for content type discovering | ||
@@ -55,2 +56,7 @@ ## Installation | ||
#### defaultCharset | ||
Type: `string` Default: `utf-8` | ||
Define the default charset encoding type if cannot infer one. | ||
#### charset | ||
@@ -57,0 +63,0 @@ Type: `boolean` Default: `true` |
@@ -28,3 +28,3 @@ var mimeware = require('../') | ||
request('http://localhost:8888', function (err, res) { | ||
expect(res.headers['content-type']).to.be.equal('text/html; charset=UTF-8') | ||
expect(res.headers['content-type']).to.be.equal('text/html; charset=utf-8') | ||
done() | ||
@@ -31,0 +31,0 @@ }) |
7784
136
73