-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathserver.js
More file actions
executable file
·60 lines (46 loc) · 1.68 KB
/
server.js
File metadata and controls
executable file
·60 lines (46 loc) · 1.68 KB
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 node
import { exec } from 'child_process'
import { createReadStream } from 'fs'
import { access } from 'fs/promises'
import { createServer } from 'http'
import os from 'os'
import { extname, join } from 'path'
let PORT = process.env.PORT ?? 3000
let ROOT_DIR = process.cwd()
let basePathname = 'flow-view'
let baseUrl = `http://localhost:${PORT}/${basePathname}/`
let fileExtensionToMimeTypeMap = new Map()
.set('css', 'text/css; charset=UTF-8')
.set('html', 'text/html; charset=UTF-8')
.set('js', 'text/javascript; charset=UTF-8')
createServer(async (req, res) => {
let url = req.url.split('/').filter((part) => part != basePathname).join('/')
let pathParts = [ROOT_DIR, url]
if (url.endsWith('/'))
pathParts.push('index.html')
let filePath = join(...pathParts)
let exists = await access(filePath).then(() => true, () => false)
let sendText = (statusCode, text) => {
res.writeHead(statusCode, { 'Content-Type': 'text/html charset=utf-8' })
res.end(text)
}
let fileExtension = extname(filePath).substring(1).toLowerCase()
let mimeType = fileExtensionToMimeTypeMap.get(fileExtension)
if (fileExtension && !mimeType)
return sendText(501, 'Not implemented')
if (!exists) {
if (mimeType) return sendText(404, 'Not found')
filePath = join(ROOT_DIR, '404.html')
mimeType = 'html'
}
let stream = createReadStream(filePath)
res.writeHead(200, { 'Content-Type': mimeType })
stream.pipe(res)
}).listen(PORT, () => {
switch(os.platform()) {
case 'darwin': exec(`open ${baseUrl}`)
case 'linux': exec(`xdg-open ${baseUrl}`)
case 'win32': exec(`start ${baseUrl}`)
default: console.info(`Server started on ${baseUrl}`)
}
})