当我编写简单的服务器并想提供静态网站时,为什么我不必像下面的示例那样使用 utf-8 编码?



为什么当我提供UTF-8编码时,为什么会出现相同的结果?

const http = require('http')
const path = require('path')
const fs = require('fs')
const server = http.createServer((req, res) => {
    if (req.url === '/') {
        fs.readFile(path.join(__dirname, 'public', 'index.html'), 'utf-8', (err, data) => {
            if (err) throw err
            res.writeHead(200, {'Content-Type': 'text/html'})
            res.end(data)
        })
    }
})
const PORT = process.env.PORT || 5000
server.listen(PORT, () => console.log('Listening on port ', PORT))

如果省略编码,则会获得一个原始的缓冲区而不是字符串。(请参阅文档(。

可以通过原始缓冲区或字符串。

相关内容

最新更新