[Pkg-javascript-commits] [node-static] 09/151: (new) serveFile
Tonnerre Lombard
tonnerre-guest at moszumanska.debian.org
Tue Jan 7 23:17:55 UTC 2014
This is an automated email from the git hooks/post-receive script.
tonnerre-guest pushed a commit to branch master
in repository node-static.
commit 7e91581e34116bf69ef231834df0ab57a9e6c73b
Author: cloudhead <self at cloudhead.net>
Date: Thu Jul 29 01:40:06 2010 -0400
(new) serveFile
---
lib/node-static.js | 33 +++++++++++++++++++++++++--------
1 file changed, 25 insertions(+), 8 deletions(-)
diff --git a/lib/node-static.js b/lib/node-static.js
index 85911ae..bb1c59e 100644
--- a/lib/node-static.js
+++ b/lib/node-static.js
@@ -40,22 +40,16 @@ this.Server = function (root, options) {
}
};
-this.Server.prototype.serve = function (req, res, callback) {
+this.Server.prototype.serveFile = function (file, req, res, callback) {
var that = this,
promise = new(events.EventEmitter);
process.nextTick(function () {
- var file = url.parse(req.url).pathname;
-
// Only allow GET and HEAD requests
if (req.method !== 'GET' && req.method !== 'HEAD') {
return finish(405, { 'Allow': 'GET, HEAD' });
}
- // If we're trying to load a directory, look for
- // an index.html inside of it.
- if (/\/$/.test(file)) { file += 'index.html' }
-
file = path.normalize(path.join(that.root, file));
// Make sure we're not trying to access a
@@ -89,10 +83,12 @@ this.Server.prototype.serve = function (req, res, callback) {
if (callback) {
callback(result);
} else {
+ if (promise.listeners('error').length > 0) {
+ promise.emit('error', result);
+ }
res.writeHead(status, headers);
res.end();
}
- promise.emit('error', result);
} else {
// Don't end the request here, if we're streaming;
// it's taken care of in `prototype.stream`.
@@ -105,6 +101,27 @@ this.Server.prototype.serve = function (req, res, callback) {
}
}
};
+this.Server.prototype.serve = function (req, res, callback) {
+ var that = this,
+ promise = new(events.EventEmitter);
+
+ process.nextTick(function () {
+ var file = url.parse(req.url).pathname;
+
+ // If we're trying to load a directory, look for
+ // an index.html inside of it.
+ if (/\/$/.test(file)) { file += 'index.html' }
+
+ that.serveFile(file, req, res, callback).addListener('success', function (result) {
+ promise.emit('success', result);
+ }).addListener('error', function (result) {
+ if (promise.listeners('error').length > 0) {
+ promise.emit('error', result);
+ }
+ });
+ });
+ return promise;
+};
this.Server.prototype.stream = function (file, stat, req, res, finish) {
var mtime = Date.parse(stat.mtime),
--
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/node-static.git
More information about the Pkg-javascript-commits
mailing list