-
Notifications
You must be signed in to change notification settings - Fork 0
/
local-docs-server.ts
69 lines (60 loc) · 2.13 KB
/
local-docs-server.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
import http from 'node:http'
import url from 'node:url'
import { access, constants, readFile, statSync } from 'node:fs'
import path from 'node:path'
import process from 'node:process'
// you can pass the parameter in the command line. e.g. node static_server.js 3000
const port = process.argv[2] || '8000'
const Console = console
// maps file extention to MIME types
// full list can be found here: https://www.freeformatter.com/mime-types-list.html
const mimeType = {
'.ico': 'image/x-icon',
'.html': 'text/html',
'.js': 'text/javascript',
'.json': 'application/json',
'.css': 'text/css',
'.png': 'image/png',
'.jpg': 'image/jpeg',
'.wav': 'audio/wav',
'.mp3': 'audio/mpeg',
'.svg': 'image/svg+xml',
'.pdf': 'application/pdf',
'.zip': 'application/zip',
'.doc': 'application/msword',
'.eot': 'application/vnd.ms-fontobject',
'.ttf': 'application/x-font-ttf',
}
http.createServer((req, res) => {
Console.info(`${req.method} ${req.url}`)
const parsedUrl = new url.URL(req.url || '/', `http://${req.headers.host}`)
const sanitizePath = path.normalize(parsedUrl.pathname || '').replace('/ez-web-audio/docs', '').replace(/^(\.\.[/\\])+/, '')
let pathname = path.join(process.cwd(), 'docs', sanitizePath)
access(pathname, constants.F_OK, (err) => {
if (err) {
// if the file is not found, return 404
res.statusCode = 404
res.end(`File ${pathname} not found!`)
return
}
// if is a directory, then look for index.html
if (statSync(pathname).isDirectory()) {
pathname += '/index.html'
}
// read file from file system
readFile(pathname, (err, data) => {
if (err) {
res.statusCode = 500
res.end(`Error getting the file: ${err}.`)
}
else {
// based on the URL path, extract the file extention. e.g. .js, .doc, ...
const ext = path.parse(pathname).ext as keyof typeof mimeType
// if the file is found, set Content-type and send data
res.setHeader('Content-type', mimeType[ext] || 'text/plain')
res.end(data)
}
})
})
}).listen(Number.parseInt(port))
Console.log(`Server listening on port ${port}`)