-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.ts
84 lines (66 loc) · 1.79 KB
/
index.ts
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
#!/usr/bin/env node
import { join } from "path"
import { createServer } from "http"
import { parse } from "url"
import chalk from "chalk"
import handle from "./serverHandling"
import { program } from "commander"
/**
* The root directory the server will serve.
*/
let root = process.cwd()
/**
* If requests should be logged in the console.
*/
let logRequests = true
/**
* If enhanced security headers should be set.
*/
let enhancedSecurity = false
/**
* If Markdown should be rendered (in HTML).
*/
let renderMarkdown = true
program.name("static-server-rdil")
program
.option("--port <number>", "the port to use", "3000")
.option("--root <string>", "the root path of the file tree to serve")
.option("--enhanced-security", "if enhanced security should be used")
.option(
"--no-request-logging",
"if requests to specific resources should be silenced"
)
.option("--no-render-markdown", "don't render markdown files as HTML")
const opts = program.parse(process.argv).opts()
/**
* The port that the server will run on.
*/
const port: number = Number.parseInt(opts.port)
if (opts.root) {
root = opts.root
}
if (opts.noRenderMarkdown) {
renderMarkdown = false
}
if (opts.enhancedSecurity) {
enhancedSecurity = true
}
if (opts.noRequestLogging) {
logRequests = false
}
/**
* The server object.
*/
const server = createServer((request, response) => {
const uriPath = parse(request.url as string).pathname as string
const filePath = join(root, unescape(uriPath))
if (logRequests) {
console.log("Serving " + uriPath)
}
handle(filePath, response, enhancedSecurity, renderMarkdown)
})
server.listen(port)
console.log(chalk`
Using working directory ${root}.
{magenta {bold Server running at http://localhost:${port}/ ⭐}}
`)