Skip to content
This repository
Browse code

One single Server exports.

  • Loading branch information...
commit a78aaba9ba32385626ea3b5748b6026fc66fd592 1 parent f1dbda9
David Sargeant dubiousdavid authored

Showing 1 changed file with 16 additions and 23 deletions. Show diff stats Hide diff stats

  1. +16 23 lib/node-static.js
39 lib/node-static.js
@@ -5,7 +5,7 @@ var fs = require('fs')
5 5 , url = require('url')
6 6 , path = require('path');
7 7
8   -exports.version = [0, 6, 5];
  8 +exports.version = [0, 6, 7];
9 9
10 10 var mime = require('./node-static/mime');
11 11 var util = require('./node-static/util');
@@ -15,7 +15,7 @@ exports.store = {};
15 15 exports.indexStore = {};
16 16 exports.mime = mime;
17 17
18   -exports.Server = function (root, options) {
  18 +Server = function (root, options) {
19 19 if (root && (typeof(root) === 'object')) { options = root; root = null }
20 20
21 21 this.root = path.resolve(root || '.');
@@ -24,7 +24,7 @@ exports.Server = function (root, options) {
24 24
25 25 this.defaultHeaders = {};
26 26 this.options.headers = this.options.headers || {};
27   -
  27 +
28 28 if ('cache' in this.options) {
29 29 if (typeof(this.options.cache) === 'number') {
30 30 this.cache = this.options.cache;
@@ -44,14 +44,14 @@ exports.Server = function (root, options) {
44 44 if (this.cache !== false) {
45 45 this.defaultHeaders['cache-control'] = 'max-age=' + this.cache;
46 46 }
47   -
  47 +
48 48 for (var k in this.defaultHeaders) {
49 49 this.options.headers[k] = this.options.headers[k] ||
50 50 this.defaultHeaders[k];
51 51 }
52 52 };
53 53
54   -exports.Server.prototype.serveDir = function (pathname, req, res, finish) {
  54 +Server.prototype.serveDir = function (pathname, req, res, finish) {
55 55 var htmlIndex = path.join(pathname, 'index.html'),
56 56 that = this;
57 57
@@ -80,10 +80,10 @@ exports.Server.prototype.serveDir = function (pathname, req, res, finish) {
80 80 }
81 81 };
82 82
83   -exports.Server.prototype.serveFile = function (pathname, status, headers, req, res) {
  83 +Server.prototype.serveFile = function (pathname, status, headers, req, res) {
84 84 var that = this;
85 85 var promise = new(events.EventEmitter);
86   -
  86 +
87 87 pathname = this.resolve(pathname);
88 88
89 89 fs.stat(pathname, function (e, stat) {
@@ -97,7 +97,7 @@ exports.Server.prototype.serveFile = function (pathname, status, headers, req, r
97 97 return promise;
98 98 };
99 99
100   -exports.Server.prototype.finish = function (status, headers, req, res, promise, callback) {
  100 +Server.prototype.finish = function (status, headers, req, res, promise, callback) {
101 101 var result = {
102 102 status: status,
103 103 headers: headers,
@@ -130,7 +130,7 @@ exports.Server.prototype.finish = function (status, headers, req, res, promise,
130 130 }
131 131 };
132 132
133   -exports.Server.prototype.servePath = function (pathname, status, headers, req, res, finish) {
  133 +Server.prototype.servePath = function (pathname, status, headers, req, res, finish) {
134 134 var that = this,
135 135 promise = new(events.EventEmitter);
136 136
@@ -163,28 +163,20 @@ exports.Server.prototype.servePath = function (pathname, status, headers, req, r
163 163 return promise;
164 164 };
165 165
166   -exports.Server.prototype.resolve = function (pathname) {
  166 +Server.prototype.resolve = function (pathname) {
167 167 return path.resolve(path.join(this.root, pathname));
168 168 };
169 169
170   -exports.Server.prototype.serve = function (req, res, callback) {
  170 +Server.prototype.serve = function (req, res, callback) {
171 171 var that = this,
172 172 promise = new(events.EventEmitter);
  173 +
  174 + var pathname = decodeURI(url.parse(req.url).pathname);
173 175
174 176 var finish = function (status, headers) {
175 177 that.finish(status, headers, req, res, promise, callback);
176 178 };
177 179
178   - var pathname;
179   - try {
180   - pathname = decodeURI(url.parse(req.url).pathname);
181   - }
182   - catch(e) {
183   - return process.nextTick(function() {
184   - return finish(400, {});
185   - });
186   - }
187   -
188 180 process.nextTick(function () {
189 181 that.servePath(pathname, 200, {}, req, res, finish).on('success', function (result) {
190 182 promise.emit('success', result);
@@ -195,7 +187,7 @@ exports.Server.prototype.serve = function (req, res, callback) {
195 187 if (! callback) { return promise }
196 188 };
197 189
198   -exports.Server.prototype.respond = function (pathname, status, _headers, files, stat, req, res, finish) {
  190 +Server.prototype.respond = function (pathname, status, _headers, files, stat, req, res, finish) {
199 191 var mtime = Date.parse(stat.mtime),
200 192 key = pathname || files[0],
201 193 headers = {},
@@ -251,7 +243,7 @@ exports.Server.prototype.respond = function (pathname, status, _headers, files,
251 243 }
252 244 };
253 245
254   -exports.Server.prototype.stream = function (pathname, files, buffer, res, callback) {
  246 +Server.prototype.stream = function (pathname, files, buffer, res, callback) {
255 247 (function streamFile(files, offset) {
256 248 var file = files.shift();
257 249
@@ -278,3 +270,4 @@ exports.Server.prototype.stream = function (pathname, files, buffer, res, callba
278 270 })(files.slice(0), 0);
279 271 };
280 272
  273 +exports.Server = Server;

0 comments on commit a78aaba

Please sign in to comment.
Something went wrong with that request. Please try again.