/
server.js
executable file
·187 lines (168 loc) · 3.78 KB
/
server.js
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
#!/usr/bin/env node
var st = require('../st.js')
var http = require('http')
var port = +(process.env.PORT || 1337)
var host = undefined
var dir = ''
var url = '/'
var cacheSize = 0
var dot = false
var index = true
var cache = true
var age = null
var cors = false
for (var i = 2; i < process.argv.length; i++) {
switch (process.argv[i]) {
case '-p':
case '--port':
port = +(process.argv[++i])
break
case '-H':
case '--host':
host = process.argv[++i]
if (host === '*') {
host = undefined
}
break
case '-l':
case '--localhost':
host = 'localhost'
break
case '-d':
case '--dir':
dir = process.argv[++i]
break
case '-u':
case '--url':
url = process.argv[++i]
break
case '-.':
case '--dot':
dot = process.argv[++i]
if (dot === undefined || dot === 'true') dot = true
else if (dot === 'false') dot = false
else if (dot.charAt(0) === '-') {
--i
dot = true
}
break
case '-n.':
case '--no-dot':
dot = false
break
case '-i':
case '--index':
index = process.argv[++i]
if (index === undefined || index === 'true') index = true
if (index === 'false') index = false
if (index.charAt(0) === '-') {
--i
index = true
}
break
case '-ni':
case '--no-index':
index = false
break
case '-h':
case '--help':
help()
process.exit(0)
case '-nc':
case '--no-cache':
cache = false
break
case '-a':
case '--age':
age = process.argv[++i]
if (isNaN(age)) {
throw new Error('invalid age: ' + JSON.stringify(age))
}
age = +age
break
case '-co':
case '--cors':
cors = true;
break
}
}
function help () {
console.log(
['st'
,'Static file server in node'
,''
,'Options:'
,''
,'-h --help Show this help'
,''
,'-p --port PORT Listen on PORT (default=1337)'
,''
,'-H --host HOST Bind address HOST (default=*)'
,''
,'-l --localhost Same as "--host localhost"'
,''
,'-d --dir DIRECTORY Serve the contents of DIRECTORY (default=cwd)'
,''
,'-u --url /url Serve at this mount url (default=/)'
,''
,'-i --index [INDEX] Use the specified INDEX filename as the result'
,' when a directory is requested. Set to "true"'
,' to turn autoindexing on, or "false" to turn it'
,' off. If no INDEX is provided, then it will turn'
,' autoindexing on. (default=true)'
,''
,'-ni --no-index Same as "--index false"'
,''
,'-. --dot [DOT] Allow .files to be served. Set to "false" to'
,' disable.'
,''
,'-n. --no-dot Same as "--dot false"'
,''
,'-co --cors Enable CORS to serve files to any domain.'
,''
,'-nc --no-cache Turn off all caching.'
,''
,'-a --age AGE Max age (in ms) of cache entries.'
].join('\n'))
}
if (isNaN(port)) throw new Error('invalid port: '+port)
var opt = {
path: dir,
url: url,
index: index,
dot: dot,
cache: {
fd: {},
stat: {},
index: {},
readdir: {},
content: {}
},
cors: cors
}
if (cache === false) {
opt.cache = false
} else {
if (age) {
Object.keys(opt.cache).forEach(function (k) {
opt.cache[k].maxAge = age
})
}
// maybe other cache-manipulating CLI flags?
}
var mount = st(opt)
http.createServer(function (q, s) {
if (mount(q, s)) return
s.statusCode = 404
s.end('not found')
}).listen(port, host, function() {
var addr = this.address()
var port = addr.port
if (!host) {
host = addr.address
}
if (/:/.test(host)) {
host = '[' + host + ']'
}
console.log('listening at http://' + host + ':' + port)
})