-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserve.js
More file actions
59 lines (50 loc) · 1.88 KB
/
Copy pathserve.js
File metadata and controls
59 lines (50 loc) · 1.88 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
// Local preview server. Resolves extensionless paths to `.html` the same way
// GitHub Pages does, so links match what the deployed site serves.
import fs from 'node:fs'
import http from 'node:http'
import path from 'node:path'
import { fileURLToPath } from 'node:url'
const ROOT = path.join(fileURLToPath(new URL('.', import.meta.url)), 'site')
const PORT = Number(process.env.PORT ?? 5173)
// The site is published under a path prefix, and 404.html links are absolute,
// so serve the same prefix locally.
const BASE = '/examples'
const TYPES = {
'.html': 'text/html; charset=utf-8',
'.css': 'text/css; charset=utf-8',
'.js': 'text/javascript; charset=utf-8',
'.json': 'application/json; charset=utf-8',
'.xml': 'application/xml; charset=utf-8',
'.txt': 'text/plain; charset=utf-8',
}
function resolve(urlPath) {
let decoded
try {
decoded = decodeURIComponent(urlPath)
} catch {
return null // malformed percent-encoding
}
const target = path.join(ROOT, decoded)
if (!target.startsWith(ROOT)) return null
for (const candidate of [target, target + '.html', path.join(target, 'index.html')]) {
if (fs.existsSync(candidate) && fs.statSync(candidate).isFile()) return candidate
}
return null
}
http.createServer((req, res) => {
const url = new URL(req.url, 'http://localhost')
if (url.pathname !== BASE && !url.pathname.startsWith(BASE + '/')) {
res.writeHead(302, { location: BASE + '/' }).end()
return
}
const file = resolve(url.pathname.slice(BASE.length))
if (!file) {
res.writeHead(404, { 'content-type': 'text/html; charset=utf-8' })
fs.createReadStream(path.join(ROOT, '404.html')).pipe(res)
return
}
res.writeHead(200, { 'content-type': TYPES[path.extname(file)] ?? 'application/octet-stream' })
fs.createReadStream(file).pipe(res)
}).listen(PORT, () => {
console.log(`Serving site/ on http://localhost:${PORT}${BASE}/`)
})