Is there any standard way of mapping a file extension to a particular middleware adapter in Express?
When a request for a file extension ending with ".map" happens in production, I'd like to return a 404 explicitly, yet in development, the file would be served/allowed (if present).
Further, I've noticed that if a "map" file (the source map file) isn't present, that the session provider still activates for the request, even when the file isn't present (which is inefficient). So, this would also help prevent unnecessary session loading/saving.
Before installing the middleware for session, I've added this code:
app.use(function(req, res, next) {
if (req && req.originalUrl) {
var originalUrl = url.parse(req.originalUrl);
var testMap = /^.*\.map$/;
if (testMap.test(originalUrl.pathname)) {
console.log("[MAP] %s %s", req.method, req.url);
res.send(404);
res.end();
return;
} else {
next();
}
}
});
While it works:
[MAP] GET /javascripts/vendor/jquery.min.map
I thought I'd be able to specify the file path (specifically the extension), using the first parameter to use? But, I can't seem to get the syntax correct (I tried the regular expression I used in above, but it doesn't seem to work).
Edit (the following are the lines above the block from my app.js file):
// all environments
app.set('port', process.env.PORT || 4000);
app.set('views', path.join(process.cwd(), 'views'));
app.set('view engine', 'dust');
app.engine('dust', dustjs.dust({
layout: 'main_layout',
cache: false
}));
app.use(express.favicon());
app.use(express.logger('dev'));
app.use(express.methodOverride());
app.use(express.cookieParser(cookieSecret));
// I've tried to move this before and after the "map" code, to no effect
app.use(express.static(path.join(__dirname, 'public')));