response.statusCode = 200
const filePath = path === '/' ? '/index.html' : path // 默认index.html
// 识别后缀
const index = filePath.lastIndexOf('.')
const suffix = filePath.substring(index)
const fileType = {
".html": "text/html",
".js": "text/javascript",
".css": "text/css",
".json": "text/json",
".png": "image/png",
".jpg": "image/jpeg"
}
response.setHeader('Content-Type',
`${fileType[suffix] || 'text/html'};charset=utf-8`)
// 处理错误
let content
try{
content = fs.readFileSync(`./public${filePath}`)
}catch(error){
content = "文件不存在"
response.statusCode = 404
}
response.write(content)
response.end()