Permalink
Browse files

added command line interface

  • Loading branch information...
1 parent 63f9385 commit c77b5cf892025e885a73ad1c6ca5c28d28e2e437 @aaronj1335 aaronj1335 committed Aug 25, 2012
Showing with 138 additions and 11 deletions.
  1. +97 −0 bin/cli.js
  2. +23 −0 etc/404.html
  3. BIN etc/trainwreck.jpg
  4. +18 −11 package.json
View
@@ -0,0 +1,97 @@
+#!/usr/bin/env node
+
+var fs = require('fs'),
+ path = require('path'),
+ tty = require('tty'),
+
+ statik = require('./../lib/node-static'),
+
+ argv = require('optimist')
+ .usage([
+ 'USAGE: $0 [-p <port>] [<directory>]',
+ 'simple, rfc 2616 compliant file streaming module for node']
+ .join('\n\n'))
+ .option('port', {
+ alias: 'p',
+ 'default': 8080,
+ description: 'TCP port at which the files will be served'
+ })
+ .option('cache', {
+ alias: 'c',
+ description: '"Cache-Control" header setting, defaults to 3600'
+ })
+ .option('version', {
+ alias: 'v',
+ description: 'node-static version'
+ })
+ .option('headers', {
+ alias: 'H',
+ description: 'additional headers (in JSON format)'
+ })
+ .option('header-file', {
+ alias: 'f',
+ description: 'JSON file of additional headers'
+ })
+ .option('help', {
+ alias: 'h',
+ description: 'display this help message'
+ })
+ .argv,
+ dir = argv._[0] || '.',
+
+ trainwreck = fs.readFileSync(path.join(__dirname, '../etc/trainwreck.jpg')),
+ notFound = fs.readFileSync(path.join(__dirname, '../etc/404.html'))
+ .toString()
+ .replace('{{trainwreck}}', trainwreck.toString('base64')),
+
+ colors = require('colors'),
+ log = function(request, response, statusCode) {
+ var d = new(Date),
+ seconds = d.getSeconds() < 10? '0'+d.getSeconds() : d.getSeconds(),
+ datestr = d.getHours() + ':' + d.getMinutes() + ':' + seconds,
+ line = datestr + ' [' + response.statusCode + ']: ' + request.url,
+ colorized = line;
+ if (tty.isatty(process.stdout.fd))
+ colorized = response.statusCode >= 500? line.red.bold :
+ response.statusCode >= 400? line.red :
+ line;
+ console.log(colorized);
+ },
+
+ file, options;
+
+if (argv.help)
+ require('optimist').showHelp(console.log),
+ process.exit(0);
+
+if (argv.version)
+ console.log('node-static', statik.version.join('.')),
+ process.exit(0);
+
+if (argv.cache)
+ (options = options || {}).cache = argv.cache;
+
+if (argv.headers)
+ (options = options || {}).headers = JSON.parse(argv.headers);
+
+if (argv['header-file'])
+ (options = options || {}).headers =
+ JSON.parse(fs.readFileSync(argv['header-file']));
+
+file = new(statik.Server)(dir, options);
+
+require('http').createServer(function (request, response) {
+ request.addListener('end', function () {
+ file.serve(request, response, function(e, rsp) {
+ if (e && e.status === 404) {
+ response.writeHead(e.status, e.headers);
+ response.end(notFound);
+ log(request, response);
+ } else {
+ log(request, response);
+ }
+ });
+ });
+}).listen(+argv.port);
+
+console.log('serving "' + dir + '" at http://127.0.0.1:' + argv.port);
View
@@ -0,0 +1,23 @@
+<!DOCTYPE html>
+<html>
+ <head>
+ <style>
+ html {
+ background: url("data:image/jpeg;base64, {{trainwreck}}") no-repeat center center fixed;
+ -webkit-background-size: cover;
+ -moz-background-size: cover;
+ -o-background-size: cover;
+ background-size: cover;
+ }
+ h1, p {
+ color: red;
+ font-family: Helvetica;
+ }
+ </style>
+ </head>
+ <body>
+ <h1>not found</h1>
+ <p>don't worry though, it could be worse.</p>
+ </body>
+</html>
+
View
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View
@@ -5,25 +5,32 @@
"keywords" : ["http", "static", "file", "server"],
"author" : "Alexis Sellier <self@cloudhead.net>",
"contributors" : [
- {
- "name": "Pablo Cantero",
- "email": "pablo@pablocantero.com"
- }
- ],
- "repository": {
+ {
+ "name": "Pablo Cantero",
+ "email": "pablo@pablocantero.com"
+ }
+ ],
+ "repository": {
"type": "git",
"url": "http://github.com/cloudhead/node-static"
},
"main" : "./lib/node-static",
"scripts": {
"test": "vows --spec --isolate"
},
+ "bin": {
+ "static": "bin/cli.js"
+ },
"license" : "MIT",
- "dependencies" : {},
- "devDependencies" : {
- "request": "latest",
- "vows": "latest"
- },
+ "dependencies" : {
+ "optimist": ">=0.3.4",
+ "colors": ">=0.6.0"
+ },
+ "devDependencies" : {
+ "request": "latest",
+ "vows": "latest"
+ },
"version" : "0.6.0",
"engines" : { "node": ">= 0.4.1" }
}
+

0 comments on commit c77b5cf

Please sign in to comment.