Permalink
Browse files

One single Server exports.

  • Loading branch information...
1 parent f1dbda9 commit a78aaba9ba32385626ea3b5748b6026fc66fd592 @dubiousdavid dubiousdavid committed Apr 1, 2013
Showing with 16 additions and 23 deletions.
  1. +16 −23 lib/node-static.js
View
@@ -5,7 +5,7 @@ var fs = require('fs')
, url = require('url')
, path = require('path');
-exports.version = [0, 6, 5];
+exports.version = [0, 6, 7];
var mime = require('./node-static/mime');
var util = require('./node-static/util');
@@ -15,7 +15,7 @@ exports.store = {};
exports.indexStore = {};
exports.mime = mime;
-exports.Server = function (root, options) {
+Server = function (root, options) {
if (root && (typeof(root) === 'object')) { options = root; root = null }
this.root = path.resolve(root || '.');
@@ -24,7 +24,7 @@ exports.Server = function (root, options) {
this.defaultHeaders = {};
this.options.headers = this.options.headers || {};
-
+
if ('cache' in this.options) {
if (typeof(this.options.cache) === 'number') {
this.cache = this.options.cache;
@@ -44,14 +44,14 @@ exports.Server = function (root, options) {
if (this.cache !== false) {
this.defaultHeaders['cache-control'] = 'max-age=' + this.cache;
}
-
+
for (var k in this.defaultHeaders) {
this.options.headers[k] = this.options.headers[k] ||
this.defaultHeaders[k];
}
};
-exports.Server.prototype.serveDir = function (pathname, req, res, finish) {
+Server.prototype.serveDir = function (pathname, req, res, finish) {
var htmlIndex = path.join(pathname, 'index.html'),
that = this;
@@ -80,10 +80,10 @@ exports.Server.prototype.serveDir = function (pathname, req, res, finish) {
}
};
-exports.Server.prototype.serveFile = function (pathname, status, headers, req, res) {
+Server.prototype.serveFile = function (pathname, status, headers, req, res) {
var that = this;
var promise = new(events.EventEmitter);
-
+
pathname = this.resolve(pathname);
fs.stat(pathname, function (e, stat) {
@@ -97,7 +97,7 @@ exports.Server.prototype.serveFile = function (pathname, status, headers, req, r
return promise;
};
-exports.Server.prototype.finish = function (status, headers, req, res, promise, callback) {
+Server.prototype.finish = function (status, headers, req, res, promise, callback) {
var result = {
status: status,
headers: headers,
@@ -130,7 +130,7 @@ exports.Server.prototype.finish = function (status, headers, req, res, promise,
}
};
-exports.Server.prototype.servePath = function (pathname, status, headers, req, res, finish) {
+Server.prototype.servePath = function (pathname, status, headers, req, res, finish) {
var that = this,
promise = new(events.EventEmitter);
@@ -163,28 +163,20 @@ exports.Server.prototype.servePath = function (pathname, status, headers, req, r
return promise;
};
-exports.Server.prototype.resolve = function (pathname) {
+Server.prototype.resolve = function (pathname) {
return path.resolve(path.join(this.root, pathname));
};
-exports.Server.prototype.serve = function (req, res, callback) {
+Server.prototype.serve = function (req, res, callback) {
var that = this,
promise = new(events.EventEmitter);
+
+ var pathname = decodeURI(url.parse(req.url).pathname);
var finish = function (status, headers) {
that.finish(status, headers, req, res, promise, callback);
};
- var pathname;
- try {
- pathname = decodeURI(url.parse(req.url).pathname);
- }
- catch(e) {
- return process.nextTick(function() {
- return finish(400, {});
- });
- }
-
process.nextTick(function () {
that.servePath(pathname, 200, {}, req, res, finish).on('success', function (result) {
promise.emit('success', result);
@@ -195,7 +187,7 @@ exports.Server.prototype.serve = function (req, res, callback) {
if (! callback) { return promise }
};
-exports.Server.prototype.respond = function (pathname, status, _headers, files, stat, req, res, finish) {
+Server.prototype.respond = function (pathname, status, _headers, files, stat, req, res, finish) {
var mtime = Date.parse(stat.mtime),
key = pathname || files[0],
headers = {},
@@ -251,7 +243,7 @@ exports.Server.prototype.respond = function (pathname, status, _headers, files,
}
};
-exports.Server.prototype.stream = function (pathname, files, buffer, res, callback) {
+Server.prototype.stream = function (pathname, files, buffer, res, callback) {
(function streamFile(files, offset) {
var file = files.shift();
@@ -278,3 +270,4 @@ exports.Server.prototype.stream = function (pathname, files, buffer, res, callba
})(files.slice(0), 0);
};
+exports.Server = Server;

0 comments on commit a78aaba

Please sign in to comment.