Skip to content

Instantly share code, notes, and snippets.

@andrewvc
Created April 6, 2010 22:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andrewvc/358229 to your computer and use it in GitHub Desktop.
Save andrewvc/358229 to your computer and use it in GitHub Desktop.
exports.deliver = function (webroot, req, res) {
var
stream,
fpRes = exports.filepath(webroot, url.parse(req.url).pathname),
fpErr = fpRes[0],
filepath = fpRes[1],
errorCallback,
headerFields = {},
addHeaderCallback,
delegate = {
error: function (callback) {
errorCallback = callback;
return delegate;
},
//...Setup the other callbacks...
};
process.nextTick(function() {
//If file is in a directory outside of the webroot, deny the request
if (fpErr) {
//... omitted for brevity...
}
else {
fs.stat(filepath, function (err, stat) {
if( (err || !stat.isFile())) {
var exactErr = err || 'File not found';
if (beforeCallback)
beforeCallback();
if (otherwiseCallback)
otherwiseCallback(exactErr);
} else {
stream = exports.streamFile(filepath, headerFields, stat, res, req)
//... omitted for brevity ...
if(errorCallback){
stream.addListener("error", errorCallback);
}
}
});
}
});
return delegate;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment