Created
May 18, 2013 05:59
-
-
Save wookiehangover/5603401 to your computer and use it in GitHub Desktop.
connect middleware that wraps serves common-js modules wrapped with AMD define()
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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