Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

[refactor] Refactor `http-server` to make API nicer

Previous API assumed using CLI application. This one makes programmatic
use easier.
  • Loading branch information...
commit 65e91f995b72348672e97dfd67525204d7960a62 1 parent ab5ec50
@mmalecki mmalecki authored
Showing with 84 additions and 82 deletions.
  1. +39 −6 bin/http-server
  2. +45 −76 lib/http-server.js
View
45 bin/http-server
@@ -1,12 +1,45 @@
#!/usr/bin/env node
-var HTTPServer = require('../lib/http-server');
+var colors = require('colors'),
+ httpServer = require('../lib/http-server'),
+ argv = require('optimist').argv;
-var httpServer = new HTTPServer();
+if (argv.h || argv.help) {
+ console.log([
+ "usage: http-server [path] [options]",
+ "",
+ "options:",
+ " -p Port to use [8080]",
+ " -a Address to use [0.0.0.0]",
+ " -i Display autoIndex [true]",
+ " -s --silent Suppress log messages from output",
+ " -h --help Print this list and exit.",
+ ].join('\n'));
+ process.exit();
+}
-httpServer.start();
+var port = argv.p || 8080,
+ host = argv.a || '0.0.0.0',
+ log = (argv.s || argv.silent) ? (function () {}) : console.log;
-process.on('SIGINT', function() {
- httpServer.log('http-server stopped.'.red);
- return process.exit();
+var options = {
+ root: argv._[0],
+ autoIndex: argv.i,
+ cache: argv.c
+};
+
+function onListening() {
+ log('Starting up http-server, serving '.yellow
+ + server.root.cyan
+ + ' on port: '.yellow
+ + port.toString().cyan);
+ log('Hit CTRL-C to stop the server');
+}
+
+var server = httpServer.createServer(options);
+server.listen(port, host, onListening);
+
+process.on('SIGINT', function () {
+ log('http-server stopped.'.red);
+ process.exit();
});
View
121 lib/http-server.js
@@ -1,78 +1,47 @@
-var flatiron = require('flatiron'),
- path = require('path'),
- argv = require('optimist').argv,
- colors = require('colors'),
- ecstatic = require('ecstatic'),
- app = flatiron.app;
-
-
-var opts = {
- root: argv._[0] || (path.existsSync("./public") ? "./public" : "./"),
- port: argv.p || 8080,
- host: argv.a || '0.0.0.0',
- cache: argv.c || 3600, // in seconds.
- autoIndex: argv.i || true,
- silent: argv.s || argv.silent || false,
- help: argv.h || argv.help
-}
-
-var showHelp = function () {
- var help = [
- "usage: http-server [path] [options]",
- "",
- "options:",
- " -p Port to use [8080]",
- " -a Address to use [0.0.0.0]",
- " -i Display autoIndex [true]",
- " -s --silent Suppress log messages from output",
- " -h --help Print this list and exit.",
- ].join('\n');
- console.log(help);
-}
-
-module.exports = function (overrides) {
- var server = this;
-
- server.start = function () {
- if (overrides) {
- Object.keys(overrides).forEach(function (k) {
- opts[k] = overrides[k];
- });
- }
-
- if (opts.help) {
- return showHelp();
+var fs = require('fs'),
+ util = require('util'),
+ flatiron = require('flatiron'),
+ ecstatic = require('ecstatic');
+
+var HTTPServer = exports.HTTPServer = function (options) {
+ options = options || {};
+
+ flatiron.App.call(this, options);
+
+ if (options.root) {
+ this.root = options.root;
+ }
+ else {
+ try {
+ fs.lstatSync('./public');
+ this.root = './public';
}
-
- server.log('Starting up http-server, serving '.yellow
- + opts.root.cyan
- + ' on port: '.yellow
- + opts.port.toString().cyan);
-
-
- //TODO: Add 404 file behavior to ecstatic, make configurable
- app.use(flatiron.plugins.http, {
- before: [
- ecstatic(opts.root, {
- autoIndex: opts.autoIndex,
- cache: opts.cache
- })
- ]
- });
-
- app.init(app.start.bind(app, opts.port, opts.host));
-
- server.log('http-server successfully started: '.green
- + 'http://'.cyan
- + opts.host.cyan
- + ':'.cyan
- + opts.port.toString().cyan);
- server.log('Hit CTRL-C to stop the server');
- };
-
- server.log = function (message) {
- if (!opts.silent) {
- console.log(message);
+ catch (err) {
+ this.root = './';
}
- };
-}
+ }
+
+ this.cache = options.cache || 3600; // in seconds.
+ this.autoIndex = options.autoIndex !== false;
+};
+util.inherits(HTTPServer, flatiron.App);
+
+HTTPServer.prototype.listen = function (port, host, callback) {
+ this.use(flatiron.plugins.http, {
+ before: [
+ ecstatic(this.root, {
+ autoIndex: this.autoIndex,
+ cache: this.cache
+ })
+ ]
+ });
+ return this.start(port, host, callback);
+};
+
+HTTPServer.prototype.close = function () {
+ return this.server.close();
+};
+
+exports.createServer = function (options) {
+ return new HTTPServer(options);
+};
Please sign in to comment.
Something went wrong with that request. Please try again.