-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhttp-server.coffee
executable file
·60 lines (54 loc) · 1.74 KB
/
http-server.coffee
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
#!/usr/bin/env coffee
yargs = require "yargs"
express = require "express"
expressTimeout = require "express-timeout-header"
fs = require "fs"
morgan = require "morgan"
path = require "path"
args = yargs
.usage("Usage: $0 [options]")
.option("d",
alias: "directory"
demand: true
describe: "The directory to server (this directory will become / in
the HTTP server"
requiresArg: true
type: "string"
)
.help("h").alias("h", "help")
.option("p",
alias: "port"
default: 8081
describe: "The HTTP server port."
requiresArg: true
)
.option("t",
alias: "timeout"
default: 10000
describe: "The maximum timeout to apply (and the default timeout if
--require-header is not set)."
requireArg: true
)
.option("require-header",
describe: "Require the Timeout header to be sent by a client to apply
timeout logic. By default, the server applies the default timeout
to all requests (to make it easier to test against unmodified
clients), but this is probably a bad idea in most cases."
type: "boolean"
)
.epilog("By Brendan Long <b.long@cablelabs.com> at CableLabs, Inc.")
.argv
app = express()
if not args.requireHeader
app.use (req, res, next) ->
if not ("timeout" in req.headers)
req.headers.timeout = args.timeout
next()
app.use(morgan(":method :url served :status in :response-time ms"))
app.use(expressTimeout(args.directory,
maxTimeout: args.timeout,
requireHeader: args.requireHeader
))
app.use(express.static(args.directory))
app.listen(args.port)
console.log("Started server at: http://localhost:" + args.port);