Permalink
Browse files

[revert] Bad option parsing code. Force pushing v0.2.3

  • Loading branch information...
1 parent f7a3676 commit cf63eb78a304ce4e29a60c47c7ea595fca2ad166 @Marak Marak committed Aug 30, 2011
Showing with 40 additions and 54 deletions.
  1. +2 −43 bin/http-server
  2. +38 −11 lib/http-server.js
View
45 bin/http-server
@@ -1,53 +1,12 @@
#!/usr/bin/env node
-var HTTPServer = require('../lib/http-server').HTTPServer,
- colors = require('colors'),
- argv = require('optimist').argv;
-
-if (argv.h || argv.help) {
- return showHelp();
-}
-
-var httpServer = new HTTPServer({
- root: argv._[0] || ".",
- port: argv.p || 8080,
- host: argv.a || 'localhost',
- cache: argv.c || 3600,
- autoIndex: argv.i || true,
- silent: argv.s || argv.silent || false
-});
-
-
-httpServer.log('Starting up http-server, serving '.yellow
- + httpServer.root.cyan
- + ' on port: '.yellow
- + httpServer.port.toString().cyan);
+var HTTPServer = require('../lib/http-server').HTTPServer;
+var httpServer = new HTTPServer();
httpServer.start();
-httpServer.log('http-server successfully started: '.green
- + 'http://'.cyan
- + httpServer.host.cyan
- + ':'.cyan
- + httpServer.port.toString().cyan);
-httpServer.log('Hit CTRL-C to stop the server')
-
process.on('SIGINT', function() {
httpServer.log('http-server stopped.'.red);
return process.exit();
});
-
-function showHelp() {
- var help = [
- "usage: http-server [path] [options]",
- "",
- "options:",
- " -p Port to use [8080]",
- " -a Address to use [localhost]",
- " -i Display autoIndex [true]",
- " -s --silent Suppress log messages from output",
- " -h --help Print this list and exit.",
- ].join('\n');
- console.log(help);
-}
View
49 lib/http-server.js
@@ -6,32 +6,46 @@
*
*/
-var static = require('../vendor/node-static/lib/node-static'),
+var colors = require('colors'),
+ argv = require('optimist').argv,
+ static = require('../vendor/node-static/lib/node-static'),
http = require('http');
var HTTPServer = exports.HTTPServer = function (options) {
- var defaults = {
- root: options.root || ".",
- port: options.port || 8080,
- host: options.host || 'localhost',
- cache: options.cache || 3600,
- autoIndex: options.autoIndex || true,
- silent: options.silent || false
- }
+ this.root = argv._[0] || ".";
+ this.port = argv.p || 8080;
+ this.host = argv.a || 'localhost';
+ this.cache = argv.c || 3600;
+ this.autoIndex = argv.i || true;
+ this.silent = argv.s || argv.silent || false;
for (var o in options) {
- this[o] = options[o] || defaults[o];
+ this[o] = options[o];
}
this.file = new(static.Server)(this.root, { autoIndex: this.autoIndex, cache: Number(this.cache) });
}
+
HTTPServer.prototype.start = function () {
var self = this;
+ if (argv.h || argv.help) {
+ return showHelp();
+ }
+ self.log('Starting up http-server, serving '.yellow
+ + self.root.cyan
+ + ' on port: '.yellow
+ + self.port.toString().cyan);
http.createServer(function(request, response) {
request.on('end', function() {
- self.log('[served] ' + request.url);
+ self.log('['.grey+'served'.yellow+'] '.grey + request.url);
return self.file.serve(request, response);
});
}).listen(self.port);
+ self.log('http-server successfully started: '.green
+ + 'http://'.cyan
+ + self.host.cyan
+ + ':'.cyan
+ + self.port.toString().cyan);
+ self.log('Hit CTRL-C to stop the server')
}
HTTPServer.prototype.log = function (message) {
@@ -40,3 +54,16 @@ HTTPServer.prototype.log = function (message) {
}
}
+function showHelp() {
+ var help = [
+ "usage: http-server [path] [options]",
+ "",
+ "options:",
+ " -p Port to use [8080]",
+ " -a Address to use [localhost]",
+ " -i Display autoIndex [true]",
+ " -s --silent Suppress log messages from output",
+ " -h --help Print this list and exit.",
+ ].join('\n');
+ console.log(help);
+}

0 comments on commit cf63eb7

Please sign in to comment.