[Pkg-javascript-commits] [node-static] 72/151: added command line interface

Tonnerre Lombard tonnerre-guest at moszumanska.debian.org
Tue Jan 7 23:17:59 UTC 2014


This is an automated email from the git hooks/post-receive script.

tonnerre-guest pushed a commit to branch master
in repository node-static.

commit c77b5cf892025e885a73ad1c6ca5c28d28e2e437
Author: Aaron Stacy <aaron.r.stacy at gmail.com>
Date:   Sat Aug 25 18:05:23 2012 -0500

    added command line interface
---
 bin/cli.js         |  97 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 etc/404.html       |  23 +++++++++++++
 etc/trainwreck.jpg | Bin 0 -> 543733 bytes
 package.json       |  29 ++++++++++------
 4 files changed, 138 insertions(+), 11 deletions(-)

diff --git a/bin/cli.js b/bin/cli.js
new file mode 100755
index 0000000..8025b94
--- /dev/null
+++ b/bin/cli.js
@@ -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);
diff --git a/etc/404.html b/etc/404.html
new file mode 100644
index 0000000..147aeb1
--- /dev/null
+++ b/etc/404.html
@@ -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>
+
diff --git a/etc/trainwreck.jpg b/etc/trainwreck.jpg
new file mode 100644
index 0000000..ac85b63
Binary files /dev/null and b/etc/trainwreck.jpg differ
diff --git a/package.json b/package.json
index f064db1..4789780 100644
--- a/package.json
+++ b/package.json
@@ -5,12 +5,12 @@
   "keywords" : ["http", "static", "file", "server"],
   "author" : "Alexis Sellier <self at cloudhead.net>",
   "contributors" : [
-		{
-			"name": "Pablo Cantero",
-			"email": "pablo at pablocantero.com"
-		}
-	],
-	"repository": {
+    {
+      "name": "Pablo Cantero",
+      "email": "pablo at pablocantero.com"
+    }
+  ],
+  "repository": {
     "type": "git",
     "url": "http://github.com/cloudhead/node-static"
   },
@@ -18,12 +18,19 @@
   "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" }
 }
+

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/node-static.git



More information about the Pkg-javascript-commits mailing list