node.js - 为什么我的 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" />
任何人都知道如何解决它?太感谢了!
解决方案
要提供静态文件,例如图像、CSS 文件和 JavaScript 文件,请使用 Express 中的 express.static 内置中间件函数
var express = require('express');
var app = express();
app.use(express.static('public'));
推荐阅读
- vue.js - 使用 tmi.js(Twitch 消息接口)获取实时聊天消息时出现性能问题
- python - 给定行索引,有没有办法在 Pandas DataFrame 中组合行?
- dbt - 使用自定义架构时,Dbt 文档详细部分为空
- javascript - 如何检测溢出是否已滚动到底部?
- sql-server - SQL Server:汇总缺少摘要
- javascript - 在 node.js 中使用异步等待函数读取 csv 文件。由于在承诺解决之前执行而未定义
- kubernetes - 我可以在 pod 配置中的哪个位置使用已定义的环境变量和 $(x)
- java - 查找字符串中的重复字符
- lua - 在 Lua 中匹配相似的字符串
- python - 这个视图函数在 Python 中使用 Flask 框架做了什么?