comparison node_modules/node-static/bin/cli.js @ 69:333afcfd3f3a

added node_modules to project and fixed path to chronometer also added deps to installer script
author tzara <rc-web@kiben.net>
date Sat, 26 Oct 2013 14:12:50 +0100
parents
children 0ae87af84e2f
comparison
equal deleted inserted replaced
68:b076cd17638c 69:333afcfd3f3a
1 #!/usr/bin/env node
2
3 var fs = require('fs'),
4 path = require('path'),
5 tty = require('tty'),
6 statik = require('./../lib/node-static');
7
8 var argv = require('optimist')
9 .usage([
10 'USAGE: $0 [-p <port>] [<directory>]',
11 'simple, rfc 2616 compliant file streaming module for node']
12 .join('\n\n'))
13 .option('port', {
14 alias: 'p',
15 'default': 8080,
16 description: 'TCP port at which the files will be served'
17 })
18 .option('cache', {
19 alias: 'c',
20 description: '"Cache-Control" header setting, defaults to 3600'
21 })
22 .option('version', {
23 alias: 'v',
24 description: 'node-static version'
25 })
26 .option('headers', {
27 alias: 'H',
28 description: 'additional headers (in JSON format)'
29 })
30 .option('header-file', {
31 alias: 'f',
32 description: 'JSON file of additional headers'
33 })
34 .option('help', {
35 alias: 'h',
36 description: 'display this help message'
37 })
38 .argv;
39
40 var dir = argv._[0] || '.';
41
42 var trainwreck = fs.readFileSync(path.join(__dirname, '../etc/trainwreck.jpg')),
43 notFound = fs.readFileSync(path.join(__dirname, '../etc/404.html'))
44 .toString()
45 .replace('{{trainwreck}}', trainwreck.toString('base64'));
46
47 var colors = require('colors');
48
49 var log = function(request, response, statusCode) {
50 var d = new Date();
51 var seconds = d.getSeconds() < 10? '0'+d.getSeconds() : d.getSeconds(),
52 datestr = d.getHours() + ':' + d.getMinutes() + ':' + seconds,
53 line = datestr + ' [' + response.statusCode + ']: ' + request.url,
54 colorized = line;
55 if (tty.isatty(process.stdout.fd))
56 colorized = (response.statusCode >= 500) ? line.red.bold :
57 (response.statusCode >= 400) ? line.red :
58 line;
59 console.log(colorized);
60 };
61
62 var file, options;
63
64 if (argv.help){
65 require('optimist').showHelp(console.log);
66 process.exit(0);
67 }
68
69 if (argv.version){
70 console.log('node-static', statik.version.join('.'));
71 process.exit(0);
72 }
73
74 if (argv.cache){
75 (options = options || {}).cache = argv.cache;
76 }
77
78 if (argv.headers){
79 (options = options || {}).headers = JSON.parse(argv.headers);
80 }
81
82 if (argv['header-file']){
83 (options = options || {}).headers =
84 JSON.parse(fs.readFileSync(argv['header-file']));
85 }
86
87 file = new(statik.Server)(dir, options);
88
89 require('http').createServer(function (request, response) {
90 request.addListener('end', function () {
91 file.serve(request, response, function(e, rsp) {
92 if (e && e.status === 404) {
93 response.writeHead(e.status, e.headers);
94 response.end(notFound);
95 log(request, response);
96 } else {
97 log(request, response);
98 }
99 });
100 }).resume();
101 }).listen(+argv.port);
102
103 console.log('serving "' + dir + '" at http://127.0.0.1:' + argv.port);
104