1 | #!/usr/bin/env node
|
2 | import * as http from 'http'
|
3 | import * as path from 'path'
|
4 | import * as fs from 'fs'
|
5 | import * as env from '../environment.js'
|
6 | import * as number from '../number.js'
|
7 | import * as logging from 'lib0/logging'
|
8 |
|
9 | const host = env.getParam('--host', 'localhost')
|
10 | const port = number.parseInt(env.getParam('--port', '8000'))
|
11 | const paramOpenFile = env.getParam('-o', '')
|
12 |
|
13 |
|
14 |
|
15 |
|
16 | const types = {
|
17 | html: 'text/html',
|
18 | css: 'text/css',
|
19 | js: 'application/javascript',
|
20 | mjs: 'application/javascript',
|
21 | png: 'image/png',
|
22 | jpg: 'image/jpeg',
|
23 | jpeg: 'image/jpeg',
|
24 | gif: 'image/gif',
|
25 | json: 'application/json',
|
26 | xml: 'application/xml'
|
27 | }
|
28 |
|
29 | const root = path.normalize(path.resolve('./'))
|
30 |
|
31 | const server = http.createServer((req, res) => {
|
32 | const url = (req.url || '/index.html').split('?')[0]
|
33 | logging.print(logging.ORANGE, logging.BOLD, req.method || '', ' ', logging.GREY, logging.UNBOLD, url)
|
34 | const extension = path.extname(url).slice(1)
|
35 | |
36 |
|
37 |
|
38 | const type = (extension && types[extension]) || types.html
|
39 | const supportedExtension = Boolean(type)
|
40 | if (!supportedExtension) {
|
41 | res.writeHead(404, { 'Content-Type': 'text/html' })
|
42 | res.end('404: File not found')
|
43 | return
|
44 | }
|
45 | let fileName = url
|
46 | if (url === '/') fileName = 'index.html'
|
47 | else if (!extension) {
|
48 | try {
|
49 | fs.accessSync(path.join(root, url + '.html'), fs.constants.F_OK)
|
50 | fileName = url + '.html'
|
51 | } catch (e) {
|
52 | fileName = path.join(url, 'index.html')
|
53 | }
|
54 | }
|
55 |
|
56 | const filePath = path.join(root, fileName)
|
57 | const isPathUnderRoot = path
|
58 | .normalize(path.resolve(filePath))
|
59 | .startsWith(root)
|
60 |
|
61 | if (!isPathUnderRoot) {
|
62 | res.writeHead(404, { 'Content-Type': 'text/html' })
|
63 | res.end('404: File not found')
|
64 | logging.print(logging.RED, logging.BOLD, 'Not Found: ', logging.GREY, logging.UNBOLD, url)
|
65 | return
|
66 | }
|
67 |
|
68 | fs.readFile(filePath, (err, data) => {
|
69 | if (err) {
|
70 | logging.print(logging.RED, logging.BOLD, 'Cannot read file: ', logging.GREY, logging.UNBOLD, url)
|
71 | res.writeHead(404, { 'Content-Type': 'text/html' })
|
72 | res.end('404: File not found')
|
73 | } else {
|
74 | res.writeHead(200, { 'Content-Type': type })
|
75 | res.end(data)
|
76 | }
|
77 | })
|
78 | })
|
79 |
|
80 | server.listen(port, host, () => {
|
81 | logging.print(logging.BOLD, logging.ORANGE, `Server is running on http://${host}:${port}`)
|
82 | if (paramOpenFile) {
|
83 | const start = process.platform === 'darwin' ? 'open' : process.platform === 'win32' ? 'start' : 'xdg-open'
|
84 | import('child_process').then(cp => {
|
85 | cp.exec(`${start} http://${host}:${port}/${paramOpenFile}`)
|
86 | })
|
87 | }
|
88 | })
|