-
Notifications
You must be signed in to change notification settings - Fork 35
/
dev.js
29 lines (26 loc) · 903 Bytes
/
dev.js
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
import { createServer } from 'node:http'
import { readFileSync, existsSync } from 'node:fs'
import { join } from 'node:path'
import { lookup } from 'mime-types'
const server = createServer((req, res) => {
if (req.url === '/') {
const indexHtml = readFileSync('index.html', 'utf8')
res.writeHead(200, { 'Content-Type': 'text/html' })
res.end(indexHtml)
} else {
const assetPath = join(process.cwd(), req.url.slice(1))
if (existsSync(assetPath)) {
const assetContent = readFileSync(assetPath)
const mimeType = lookup(assetPath)
res.writeHead(200, { 'Content-Type': mimeType })
res.end(assetContent)
} else {
const notFoundHtml = readFileSync('404.html', 'utf8')
res.writeHead(404, { 'Content-Type': 'text/html' })
res.end(notFoundHtml)
}
}
})
server.listen(3000, () => {
console.log(`Listening on http://localhost:3000`)
})