ecdab3fa0c347db0ab4b1466da6e560be6ed350b
[VSoRC/.git] / node_modules / .bin / static
1 #!/usr/bin/env node
2
3 var fs = require('fs'),
4     tty = require('tty'),
5     statik = require('./../lib/node-static');
6
7     var argv = require('optimist')
8         .usage([
9             'USAGE: $0 [-p <port>] [<directory>]',
10             'simple, rfc 2616 compliant file streaming module for node']
11             .join('\n\n'))
12         .option('port', {
13             alias: 'p',
14             'default': 8080,
15             description: 'TCP port at which the files will be served'
16         })
17         .option('host-address', {
18             alias: 'a',
19             'default': '127.0.0.1',
20             description: 'the local network interface at which to listen'
21         })
22         .option('cache', {
23             alias: 'c',
24             description: '"Cache-Control" header setting, defaults to 3600'
25         })
26         .option('version', {
27             alias: 'v',
28             description: 'node-static version'
29         })
30         .option('headers', {
31             alias: 'H',
32             description: 'additional headers (in JSON format)'
33         })
34         .option('header-file', {
35             alias: 'f',
36             description: 'JSON file of additional headers'
37         })
38         .option('gzip', {
39             alias: 'z',
40             description: 'enable compression (tries to serve file of same name plus \'.gz\')'
41         })
42         .option('spa', {
43             description: 'serve the content as a single page app by redirecting all non-file requests to the index html file'
44         })
45         .option('indexFile', {
46             alias: 'i',
47             'default': 'index.html',
48             description: 'specify a custom index file when serving up directories'
49         })
50         .option('help', {
51             alias: 'h',
52             description: 'display this help message'
53         })
54         .argv;
55
56     var dir = argv._[0] || '.';
57
58     var colors = require('colors');
59
60     var log = function(request, response, statusCode) {
61         var d = new Date();
62         var seconds = d.getSeconds() < 10? '0'+d.getSeconds() : d.getSeconds(),
63             datestr = d.getHours() + ':' + d.getMinutes() + ':' + seconds,
64             line = datestr + ' [' + response.statusCode + ']: ' + request.url,
65             colorized = line;
66         if (tty.isatty(process.stdout.fd))
67             colorized = (response.statusCode >= 500) ? line.red.bold :
68                         (response.statusCode >= 400) ? line.red :
69                         line;
70         console.log(colorized);
71     };
72
73     var file, options;
74
75 if (argv.help) {
76     require('optimist').showHelp(console.log);
77     process.exit(0);
78 }
79
80 if (argv.version) {
81     console.log('node-static', statik.version.join('.'));
82     process.exit(0);
83 }
84
85 if (argv.cache) {
86     (options = options || {}).cache = argv.cache;
87 }
88
89 if (argv.headers) {
90     (options = options || {}).headers = JSON.parse(argv.headers);
91 }
92
93 if (argv['header-file']) {
94     (options = options || {}).headers =
95         JSON.parse(fs.readFileSync(argv['header-file']));
96 }
97
98 if (argv.gzip) {
99     (options = options || {}).gzip = true;
100 }
101
102 if (argv.indexFile) {
103     (options = options || {}).indexFile = argv['indexFile'];
104 }
105
106 file = new(statik.Server)(dir, options);
107
108 require('http').createServer(function (request, response) {
109     request.addListener('end', function () {
110         var callback = function(e, rsp) {
111           if (e && e.status === 404) {
112               response.writeHead(e.status, e.headers);
113               response.end("Not Found");
114               log(request, response);
115           } else {
116               log(request, response);
117           }
118         };
119
120         if (argv['spa'] && request.url.indexOf(".") == -1) {
121             file.serveFile(argv['indexFile'], 200, {}, request, response);
122         } else {
123             file.serve(request, response, callback);
124         }
125     }).resume();
126 }).listen(+argv.port, argv['host-address']);
127
128 console.log('serving "' + dir + '" at http://' + argv['host-address'] + ':' + argv.port);
129 if (argv.spa) {
130   console.log('serving as a single page app (all non-file requests redirect to ' + argv['indexFile'] +')');
131 }