-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathserver.mjs
55 lines (49 loc) · 1.45 KB
/
server.mjs
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
import { createServer } from 'http'
import { lstatSync, readFileSync, existsSync, readFile } from 'fs'
import { join, extname, dirname } from 'path'
import { fileURLToPath } from 'url'
const PORT = 8080
const server = createServer(async (req, res) => {
const __filename = fileURLToPath(import.meta.url)
const __dirname = dirname(__filename)
const requestedUrl = new URL(req.url, `http://${req.headers.host}`)
const path = requestedUrl.pathname
const baseDirectory = join(__dirname, 'dist')
if (path === '/') {
res.writeHead(302, { Location: '/index.html' })
res.end()
} else {
const requestedFile = join(baseDirectory, path.substring(1))
try {
if (existsSync(requestedFile) && lstatSync(requestedFile).isFile()) {
const contentType = getContentType(requestedFile)
const data = readFileSync(requestedFile)
res.writeHead(200, { 'Content-Type': contentType })
res.end(data)
} else {
throw new Error('File not found')
}
} catch (err) {
res.writeHead(404)
res.end('File not found')
}
}
})
function getContentType(filePath) {
const ext = extname(filePath)
switch (ext) {
case '.html':
return 'text/html'
case '.js':
return 'text/javascript'
case '.css':
return 'text/css'
case '.png':
return 'image/png'
default:
return 'text/plain'
}
}
server.listen(PORT, () => {
console.log(`Server running on port ${PORT}`)
})