Access bundled node.js module

I wrote a small express.js based web app. I serve a manifest.webapp but express.js doesn't send the correct mime type. But because express.js bundles the mime module require("mime").define(...) has no effect. It defines the mime type for the wrong module!

Is there a way to access the mime module that is bundled with express.js? Or is there a way to tell npm (and nodejitsu) to not use the bundled modules of express.js?

I think there are two ways to fix this.

Use a simple middleware

app.get('*.webapp', function (req, res, next) {
  res.header('Content-Type', 'application/x-web-app-manifest+json');
  next();
});

Monkey patching

Modify the response object and overwrite the setHeader method, where you can modify the response the actual value (it will work as a middleware).

var mime = require('mime');
mime.define({ 'application/x-web-app-manifest+json': [ 'webapp' ] });

app.use(function (req, res, next) {
  var setHeader = res.setHeader;

  res.setHeader = function (field, val) {
    // you can be more strict here
    if (field === 'Content-Type') {
      val = mime.lookup(req.path);
    }

    return setHeader.call(this, field, val);
  }

  next();
});
app.use(express.static(__dirname + 'your-static-dir'));

So you'll use your own mime module instead of the bundled one.

Update static middleware is not using the req.contentType method.

I found out that you can access bundled modules this way:

require("express/node_modules/mime").define({
    'application/x-web-app-manifest+json': ['webapp']
});

I don't know if this is the proper way to do it, but I use this for now.