首页 > 解决方案 > 为什么我的 CSS 格式在节点中不起作用?

问题描述

我的节点代码遇到了一些问题。所以问题是我想在端口 3000 上创建一个本地服务器。在我链接我的 html 代码之后

app.get("/",(req,res,next)=> {
    res.sendfile(path.join(__dirname+'/src'+'/index.html'));
})

我的 chrome 浏览器显示:

Refused to apply style from 'http://localhost:3000/app.css' because its MIME type ('text/html') is not a supported stylesheet MIME type, and strict MIME checking is enabled.

另外,我的 CSS 不工作。这是我的 HTML 中的 CSS 标记

<link href="./app.css" rel="stylesheet" type="text/css" />

任何人都知道如何解决它?太感谢了!

标签: node.js

解决方案


要提供静态文件,例如图像、CSS 文件和 JavaScript 文件,请使用 Express 中的 express.static 内置中间件函数

var express = require('express');
var app = express();

app.use(express.static('public'));

阅读有关在 Express 中提供静态文件的更多信息


推荐阅读