index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
</head>
<body>
<h1>Hello, World!</h1>
</body>
</html>
index.js
import http from 'http'
import chalk from 'chalk'
import fs from 'fs/promises'
const port = 3000
const server = http.createServer(async (req, res) => {
try {
const data = await fs.readFile('index.html')
res.statusCode = 200 // OK
res.write(data)
} catch (e) {
res.statusCode = 500 // Internal Server Error
res.write(e.message)
} finally {
res.end()
}
})
server.listen(port, () => {
console.log(chalk.green(`Server listening at <http://localhost>:${port}`))
})