Skip to content

Commit

Permalink
Merge 1980f29 into 0e87caf
Browse files Browse the repository at this point in the history
  • Loading branch information
rvagg committed Nov 10, 2019
2 parents 0e87caf + 1980f29 commit 26e228d
Show file tree
Hide file tree
Showing 23 changed files with 1,036 additions and 977 deletions.
141 changes: 75 additions & 66 deletions bin/server.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
#!/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++) {
const st = require('../st.js')
const http = require('http')
let port = +(process.env.PORT || 1337)
let host
let dir = ''
let url = '/'
let dot = false
let index = true
let cache = true
let age = null
let cors = false

for (let i = 2; i < process.argv.length; i++) {
switch (process.argv[i]) {
case '-p':
case '--port':
Expand Down Expand Up @@ -45,9 +44,11 @@ for (var i = 2; i < process.argv.length; i++) {
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) === '-') {
if (dot === undefined || dot === 'true') {
dot = true
} else if (dot === 'false') {
dot = false
} else if (dot.charAt(0) === '-') {
--i
dot = true
}
Expand All @@ -61,9 +62,11 @@ for (var i = 2; i < process.argv.length; i++) {
case '-i':
case '--index':
index = process.argv[++i]
if (index === undefined || index === 'true') index = true
if (index === 'false') index = false
if (index.charAt(0) === '-') {
if (index === undefined || index === 'true') {
index = true
} else if (index === 'false') {
index = false
} else if (index.charAt(0) === '-') {
--i
index = true
}
Expand Down Expand Up @@ -95,54 +98,56 @@ for (var i = 2; i < process.argv.length; i++) {

case '-co':
case '--cors':
cors = true;
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'))
['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)
if (isNaN(port)) {
throw new Error('invalid port: ' + port)
}

var opt = {
const opt = {
path: dir,
url: url,
index: index,
Expand All @@ -161,27 +166,31 @@ if (cache === false) {
opt.cache = false
} else {
if (age) {
Object.keys(opt.cache).forEach(function (k) {
for (const k in opt.cache) {
opt.cache[k].maxAge = age
})
}
}
// maybe other cache-manipulating CLI flags?
}

var mount = st(opt)
const mount = st(opt)

http.createServer(function (q, s) {
if (mount(q, s)) return
if (mount(q, s)) {
return
}
s.statusCode = 404
s.end('not found')
}).listen(port, host, function() {
var addr = this.address()
var port = addr.port
}).listen(port, host, function () {
const addr = this.address()
const port = addr.port

if (!host) {
host = addr.address
}
if (/:/.test(host)) {
host = '[' + host + ']'
}

console.log('listening at http://' + host + ':' + port)
})
20 changes: 11 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,24 @@
"main": "st.js",
"bin": "bin/server.js",
"dependencies": {
"async-cache": "~1.1.0",
"bl": "~1.2.1",
"async-cache": "^1.1.0",
"bl": "^4.0.0",
"fd": "~0.0.2",
"mime": "~1.4.1",
"negotiator": "~0.6.1"
"mime": "^2.4.4",
"negotiator": "~0.6.2"
},
"optionalDependencies": {
"graceful-fs": "~4.1.11"
"graceful-fs": "^4.2.3"
},
"devDependencies": {
"request": "~2.83.0",
"rimraf": "~2.6.2",
"tap": "~10.7.2"
"request": "^2.88.0",
"rimraf": "^3.0.0",
"standard": "~14.3.1",
"tap": "^14.9.2"
},
"scripts": {
"test": "tap test/*.js test/cli/*-test.js"
"lint": "standard",
"test": "npm run lint && tap test/*.js test/cli/*-test.js"
},
"repository": {
"type": "git",
Expand Down

0 comments on commit 26e228d

Please sign in to comment.