Skip to content

Commit 719af22

Browse files
committed
Updated web tool. Vendored node-static build with AutoIndex option
1 parent 7239276 commit 719af22

File tree

11 files changed

+711
-5
lines changed

11 files changed

+711
-5
lines changed

config.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{"git":{"authors":{"cr":"Charlie Robins","hi":"h1jinx","ei":"Elijah Insua","mr":"Marak Squires"},"email":"pairing@nodejitsu.com"}}
1+
{"git":{"authors":{"cr":"Charlie Robbins","hi":"h1jinx","ei":"Elijah Insua","mr":"Marak Squires"},"email":"pairing@nodejitsu.com"}}

lib/tools/web.coffee

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
# Base64 encode / decode tool
22

33
utils = require '../utils'
4-
static = require 'node-static'
4+
static = require '../../vendor/node-static/lib/node-static'
55
http = require 'http'
66
exports.exec = (options, callback) ->
77

88
options.p = options.p || 8080
9-
root = options._[1] || __dirname
10-
console.log root
9+
root = options._[1] || '.'
1110
file = new(static.Server)(root, { AutoIndex: true, cache: 3600 });
1211
console.log 'Starting up static web server for: '.yellow + root.cyan + ' on port: '.yellow + options.p.toString().cyan
1312

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@
1616
},
1717
"dependencies": {
1818
"coffee-script": ">= 1.0.1",
19-
"optimist": ">= 0.1.3"
19+
"optimist": ">= 0.1.3",
20+
"nconf": ">= 0.1.3"
2021
},
2122
"engines": {
2223
"node": ">= 0.3.4"

vendor/node-static/LICENSE

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
Copyright (c) 2010 Alexis Sellier
2+
3+
Permission is hereby granted, free of charge, to any person obtaining
4+
a copy of this software and associated documentation files (the
5+
"Software"), to deal in the Software without restriction, including
6+
without limitation the rights to use, copy, modify, merge, publish,
7+
distribute, sublicense, and/or sell copies of the Software, and to
8+
permit persons to whom the Software is furnished to do so, subject to
9+
the following conditions:
10+
11+
The above copyright notice and this permission notice shall be
12+
included in all copies or substantial portions of the Software.
13+
14+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

vendor/node-static/README.md

Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
node-static
2+
===========
3+
4+
> a simple, *rfc 2616 compliant* file streaming module for [node](http://nodejs.org)
5+
6+
node-static has an in-memory file cache, making it highly efficient.
7+
node-static understands and supports *conditional GET* and *HEAD* requests.
8+
node-static was inspired by some of the other static-file serving modules out there,
9+
such as node-paperboy and antinode.
10+
11+
synopsis
12+
--------
13+
14+
var static = require('node-static');
15+
16+
//
17+
// Create a node-static server instance to serve the './public' folder
18+
//
19+
var file = new(static.Server)('./public');
20+
21+
require('http').createServer(function (request, response) {
22+
request.addListener('end', function () {
23+
//
24+
// Serve files!
25+
//
26+
file.serve(request, response);
27+
});
28+
}).listen(8080);
29+
30+
API
31+
---
32+
33+
### Creating a node-static Server #
34+
35+
Creating a file server instance is as simple as:
36+
37+
new static.Server();
38+
39+
This will serve files in the current directory. If you want to serve files in a specific
40+
directory, pass it as the first argument:
41+
42+
new static.Server('./public');
43+
44+
You can also specify how long the client is supposed to cache the files node-static serves:
45+
46+
new static.Server('./public', { cache: 3600 });
47+
48+
This will set the `Cache-Control` header, telling clients to cache the file for an hour.
49+
This is the default setting.
50+
51+
### Serving files under a directory #
52+
53+
To serve files under a directory, simply call the `serve` method on a `Server` instance, passing it
54+
the HTTP request and response object:
55+
56+
var fileServer = new static.Server('./public');
57+
58+
require('http').createServer(function (request, response) {
59+
request.addListener('end', function () {
60+
fileServer.serve(request, response);
61+
});
62+
}).listen(8080);
63+
64+
### Serving specific files #
65+
66+
If you want to serve a specific file, like an error page for example, use the `serveFile` method:
67+
68+
fileServer.serveFile('/error.html', 500, {}, request, response);
69+
70+
This will serve the `error.html` file, from under the file root directory, with a `500` status code.
71+
For example, you could serve an error page, when the initial request wasn't found:
72+
73+
require('http').createServer(function (request, response) {
74+
request.addListener('end', function () {
75+
fileServer.serve(request, response, function (e, res) {
76+
if (e && (e.status === 404)) { // If the file wasn't found
77+
fileServer.serveFile('/not-found.html', request, response);
78+
}
79+
});
80+
});
81+
}).listen(8080);
82+
83+
More on intercepting errors bellow.
84+
85+
### Intercepting errors & Listening #
86+
87+
An optional callback can be passed as last argument, it will be called every time a file
88+
has been served successfully, or if there was an error serving the file:
89+
90+
var fileServer = new static.Server('./public');
91+
92+
require('http').createServer(function (request, response) {
93+
request.addListener('end', function () {
94+
fileServer.serve(request, response, function (err, result) {
95+
if (err) { // There was an error serving the file
96+
sys.error("Error serving " + request.url + " - " + err.message);
97+
98+
// Respond to the client
99+
response.writeHead(err.status, err.headers);
100+
response.end();
101+
}
102+
});
103+
});
104+
}).listen(8080);
105+
106+
Note that if you pass a callback, and there is an error serving the file, node-static
107+
*will not* respond to the client. This gives you the opportunity to re-route the request,
108+
or handle it differently.
109+
110+
For example, you may want to interpret a request as a static request, but if the file isn't found,
111+
send it to an application.
112+
113+
If you only want to *listen* for errors, you can use *event listeners*:
114+
115+
fileServer.serve(request, response).addListener('error', function (err) {
116+
sys.error("Error serving " + request.url + " - " + err.message);
117+
});
118+
119+
With this method, you don't have to explicitly send the response back, in case of an error.
120+
121+
### Options when creating an instance of `Server` #
122+
123+
#### `cache` #
124+
125+
Sets the `Cache-Control` header.
126+
127+
example: `{ cache: 7200 }`
128+
129+
Passing a number will set the cache duration to that number of seconds.
130+
Passing `false` will disable the `Cache-Control` header.
131+
132+
> Defaults to `3600`
133+
134+
#### `headers` #
135+
136+
Sets response headers.
137+
138+
example: `{ 'X-Hello': 'World!' }`
139+
140+
> Defaults to `{}`
141+
142+
#### `AutoIndex` #
143+
144+
Automatically generates an html index page for directory listings
145+
146+
example: `{ 'AutoIndex': true }`
147+
148+
> Defaults to `false`
149+
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
This is ApacheBench, Version 2.3 <$Revision: 655654 $>
2+
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
3+
Licensed to The Apache Software Foundation, http://www.apache.org/
4+
5+
Benchmarking 127.0.0.1 (be patient)
6+
7+
8+
Server Software: node-static/0.3.0
9+
Server Hostname: 127.0.0.1
10+
Server Port: 8080
11+
12+
Document Path: /lib/node-static.js
13+
Document Length: 6038 bytes
14+
15+
Concurrency Level: 20
16+
Time taken for tests: 2.323 seconds
17+
Complete requests: 10000
18+
Failed requests: 0
19+
Write errors: 0
20+
Total transferred: 63190000 bytes
21+
HTML transferred: 60380000 bytes
22+
Requests per second: 4304.67 [#/sec] (mean)
23+
Time per request: 4.646 [ms] (mean)
24+
Time per request: 0.232 [ms] (mean, across all concurrent requests)
25+
Transfer rate: 26563.66 [Kbytes/sec] received
26+
27+
Connection Times (ms)
28+
min mean[+/-sd] median max
29+
Connect: 0 0 0.2 0 3
30+
Processing: 1 4 1.4 4 28
31+
Waiting: 1 4 1.3 4 18
32+
Total: 2 5 1.5 4 28
33+
34+
Percentage of the requests served within a certain time (ms)
35+
50% 4
36+
66% 5
37+
75% 5
38+
80% 5
39+
90% 5
40+
95% 6
41+
98% 8
42+
99% 9
43+
100% 28 (longest request)
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
var sys = require('sys');
2+
var static = require('../lib/node-static');
3+
4+
//
5+
// Create a node-static server to serve the current directory
6+
//
7+
var file = new(static.Server)('.', { cache: 7200, headers: {'X-Hello':'World!'} });
8+
9+
require('http').createServer(function (request, response) {
10+
request.addListener('end', function () {
11+
//
12+
// Serve files!
13+
//
14+
file.serve(request, response, function (err, res) {
15+
if (err) { // An error as occured
16+
sys.error("> Error serving " + request.url + " - " + err.message);
17+
response.writeHead(err.status, err.headers);
18+
response.end();
19+
} else { // The file was served successfully
20+
sys.puts("> " + request.url + " - " + res.message);
21+
}
22+
});
23+
});
24+
}).listen(8080);
25+
26+
sys.puts("> node-static is listening on http://127.0.0.1:8080");

0 commit comments

Comments
 (0)