Skip to content

Commit 234e963

Browse files
committed
[merge] Merged in flatiron refactor
2 parents 1276578 + 4a30393 commit 234e963

File tree

15 files changed

+88
-790
lines changed

15 files changed

+88
-790
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ This will install `http-server` globally so that it may be run from the command
2020

2121
http-server [path] [options]
2222

23+
`[path]` defaults to `./public` if the folder exists, and `./` otherwise.
24+
2325
# Installing as a node app
2426

2527
mkdir myapp

bin/http-server

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
#!/usr/bin/env node
22

3-
var HTTPServer = require('../lib/http-server');
4-
5-
var httpServer = new HTTPServer();
3+
var httpServer = require('../lib/http-server');
64

75
httpServer.start();
86

lib/http-server.js

Lines changed: 58 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -1,74 +1,23 @@
1-
/*
2-
*
3-
* http-server.js - A simple static HTTP server.
4-
*
5-
* (c) 2011 Nodejitsu Inc.
6-
*
7-
*/
8-
9-
var colors = require('colors'),
1+
var flatiron = require('flatiron'),
2+
path = require('path'),
103
argv = require('optimist').argv,
11-
static = require('node-static'),
12-
fs = require("fs");
13-
http = require('http');
4+
colors = require('colors'),
5+
ecstatic = require('ecstatic'),
6+
app = flatiron.app;
147

15-
var HTTPServer = module['exports'] = function (options) {
16-
this.root = argv._[0] || "./public";
17-
this.port = argv.p || 8080;
18-
this.host = argv.a || 'localhost';
19-
this.cache = argv.c || 3600;
20-
this.autoIndex = argv.i || true;
21-
this.silent = argv.s || argv.silent || false;
22-
for (var o in options) {
23-
this[o] = options[o];
24-
}
25-
this.file = new(static.Server)(this.root, { autoIndex: this.autoIndex, cache: Number(this.cache) });
26-
}
8+
var server = module.exports;
279

28-
29-
HTTPServer.prototype.start = function () {
30-
var self = this;
31-
if (argv.h || argv.help) {
32-
return showHelp();
33-
}
34-
self.log('Starting up http-server, serving '.yellow
35-
+ self.root.cyan
36-
+ ' on port: '.yellow
37-
+ self.port.toString().cyan);
38-
self.server = http.createServer(function(request, response) {
39-
request.on('end', function() {
40-
self.log('['.grey+'served'.yellow+'] '.grey + request.url);
41-
return self.file.serve(request, response, function (err, result) {
42-
if (err && err.status == 404) {
43-
// Catch case where there is no 404 file
44-
fs.stat(self.root + "/404.html", function (err, stat) {
45-
if (!err && stat.isFile()) {
46-
self.file.serveFile("/404.html", err.status, {}, request, response);
47-
} else {
48-
response.writeHead(404);
49-
response.end();
50-
}
51-
});
52-
}
53-
});
54-
});
55-
});
56-
self.server.listen(self.port);
57-
self.log('http-server successfully started: '.green
58-
+ 'http://'.cyan
59-
+ self.host.cyan
60-
+ ':'.cyan
61-
+ self.port.toString().cyan);
62-
self.log('Hit CTRL-C to stop the server')
63-
}
64-
65-
HTTPServer.prototype.log = function (message) {
66-
if (!this.silent) {
67-
console.log(message);
68-
}
10+
var opts = {
11+
root: argv._[0] || (path.existsSync("./public") ? "./public" : "./"),
12+
port: argv.p || 8080,
13+
host: argv.a || 'localhost',
14+
cache: argv.c || 3600, // in seconds.
15+
autoIndex: argv.i || true,
16+
silent: argv.s || argv.silent || false,
17+
help: argv.h || argv.help
6918
}
7019

71-
function showHelp() {
20+
var showHelp = function () {
7221
var help = [
7322
"usage: http-server [path] [options]",
7423
"",
@@ -81,3 +30,46 @@ function showHelp() {
8130
].join('\n');
8231
console.log(help);
8332
}
33+
34+
server.start = function (overrides) {
35+
if (overrides) {
36+
Object.keys(overrides).forEach(function (k) {
37+
opts[k] = overrides[k];
38+
});
39+
}
40+
41+
if (opts.help) {
42+
return showHelp();
43+
}
44+
45+
server.log('Starting up http-server, serving '.yellow
46+
+ opts.root.cyan
47+
+ ' on port: '.yellow
48+
+ opts.port.toString().cyan);
49+
50+
51+
//TODO: Add 404 file behavior to ecstatic, make configurable
52+
app.use(flatiron.plugins.http, {
53+
before: [
54+
ecstatic(opts.root, {
55+
autoIndex: opts.autoIndex,
56+
cache: opts.cache
57+
})
58+
]
59+
});
60+
61+
app.init(app.start.bind(app, opts.port, opts.host));
62+
63+
server.log('http-server successfully started: '.green
64+
+ 'http://'.cyan
65+
+ opts.host.cyan
66+
+ ':'.cyan
67+
+ opts.port.toString().cyan);
68+
server.log('Hit CTRL-C to stop the server');
69+
}
70+
71+
server.log = function (message) {
72+
if (!opts.silent) {
73+
console.log(message);
74+
}
75+
}

node_modules/node-static/LICENSE

Lines changed: 0 additions & 20 deletions
This file was deleted.

node_modules/node-static/README.md

Lines changed: 0 additions & 149 deletions
This file was deleted.

node_modules/node-static/benchmark/node-static-0.3.0.txt

Lines changed: 0 additions & 43 deletions
This file was deleted.

node_modules/node-static/examples/file-server.js

Lines changed: 0 additions & 26 deletions
This file was deleted.

0 commit comments

Comments
 (0)