Skip to content

Instantly share code, notes, and snippets.

@wookiehangover
Created May 18, 2013 05:59
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wookiehangover/5603401 to your computer and use it in GitHub Desktop.
Save wookiehangover/5603401 to your computer and use it in GitHub Desktop.
connect middleware that wraps serves common-js modules wrapped with AMD define()
var fs = require('fs');
var path = require('path');
var url = require('url');
module.exports = function(directory, blacklist) {
if( directory === undefined ) {
throw new Error('You need to pass a location to work with');
}
blacklist = blacklist || [];
return function(req, res, next) {
var filename = url.parse(req.url).path;
if( blacklist.indexOf(filename) > -1 ){
return next();
}
filename = path.join(directory, filename);
res.setHeader('conent-type', 'application/javascript');
res.write('define(function(require, exports, module){\n\n');
fs.createReadStream(filename)
.on('data', res.write.bind(res))
.on('end', function() {
res.write('\n});');
res.end();
})
.on('error', function(err) {
console.error(err);
console.trace();
next();
});
};
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment