forked from cloudhead/node-static
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli.js
More file actions
executable file
·131 lines (114 loc) · 3.96 KB
/
Copy pathcli.js
File metadata and controls
executable file
·131 lines (114 loc) · 3.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#!/usr/bin/env node
var fs = require('fs'),
tty = require('tty'),
statik = require('./../lib/node-static');
var 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('host-address', {
alias: 'a',
'default': '127.0.0.1',
description: 'the local network interface at which to listen'
})
.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('gzip', {
alias: 'z',
description: 'enable compression (tries to serve file of same name plus \'.gz\')'
})
.option('spa', {
description: 'serve the content as a single page app by redirecting all non-file requests to the index html file'
})
.option('indexFile', {
alias: 'i',
'default': 'index.html',
description: 'specify a custom index file when serving up directories'
})
.option('help', {
alias: 'h',
description: 'display this help message'
})
.argv;
var dir = argv._[0] || '.';
var colors = require('colors');
var log = function(request, response, statusCode) {
var d = new Date();
var 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);
};
var 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']));
}
if (argv.gzip) {
(options = options || {}).gzip = true;
}
if (argv.indexFile) {
(options = options || {}).indexFile = argv['indexFile'];
}
file = new(statik.Server)(dir, options);
require('http').createServer(function (request, response) {
request.addListener('end', function () {
var callback = function(e, rsp) {
if (e && e.status === 404) {
response.writeHead(e.status, e.headers);
response.end("Not Found");
log(request, response);
} else {
log(request, response);
}
};
if (argv['spa'] && request.url.indexOf(".") == -1) {
file.serveFile(argv['indexFile'], 200, {}, request, response);
} else {
file.serve(request, response, callback);
}
}).resume();
}).listen(+argv.port, argv['host-address']);
console.log('serving "' + dir + '" at http://' + argv['host-address'] + ':' + argv.port);
if (argv.spa) {
console.log('serving as a single page app (all non-file requests redirect to ' + argv['indexFile'] +')');
}