I wanted this too, so I knocked up a quick way to 404 requests with mismatching case.
It is not particularly efficient, so I only run it in development. It only checks the filename. It doesn't check the case of the folders above the file.
How to use it:
var express = require('express');
var app = express();
// You can do this before or after the express() call
// But it must come before express.static() is called
var inDevelopment = (process.NODE_ENV || 'local') === 'local';
if (inDevelopment) {
require('./modules/makeExpressStaticCaseSensitive')(express);
}
app.use(express.static(path.join(__dirname, 'public_html')));
The script module/makeExpressStaticCaseSensitive.js
module.exports = function (express) {
var fs = require('fs')
var pathlib = require('path');
var parseUrl = require('express/node_modules/parseurl')
var oldStatic = express.static;
var newStatic = function (root, options) {
var opts = Object.create(options || null);
var originalHandler = oldStatic(root, options);
var wrappedHandler = function (req, res, next) {
var filepath = pathlib.join(root, parseUrl(req).pathname);
var dirpath = pathlib.dirname(filepath);
var filename = pathlib.basename(filepath);
// @todo Reading the entire directory listing and then searching it is quite inefficient for large folders
// We should find a more efficient way to do this for one file at a time
fs.readdir(dirpath, function (err, files) {
if (err) return next(err);
var fileIsThere = files.indexOf(filename) >= 0;
if (fileIsThere) {
originalHandler(req, res, next);
} else {
res.status(404).end();
}
});
};
return wrappedHandler;
};
express.static = newStatic;
};
I have written a more efficient version which caches the output of readdir()
for a few seconds, and check the entire path, but it is somewhat longer.