nodejs使用http和fs模块访问静态资源文件,html页面样式出错

const http = require('http');
const url = require('url');
const path = require('path');
const fs = require('fs');
const app = http.createServer();
app.on('request', (req, res) => {
    res.writeHead(200, {
        'content-type': 'text/html;charset=utf8',
    })
    //获取用户的请求路径
    let pathname = url.parse(req.url).pathname;
    //将用户的请求路径转化为实际的服务器硬盘路径
    let realPath = path.join(__dirname, 'public' + pathname);
    //读取文件
    fs.readFile(realPath, (error, result) => {
        if (error != null) {
            res.end('文件读取失败');
        } else {
            res.end(result);
        }
    })
});
app.listen(3000);
console.log('服务器启动成功');

正常页面如下

文件路径

 

f12检查样式引入了吗,console,和network有报错吗

这个问题已经解决了,文件类型不匹配,要使用mime.getType(realPath),获取当前文件类型