请帮助我,因为我整天都在尝试添加 css 文件,但总是失败 app.js 代码:
var express = require("express");
var app = express();
var path = require('path');
app.set('view engine', 'ejs');
app.set('views', '../node_modules/express/views/');
app.use(express.static('public'));
app.get("/",function(req,res){
console.log("welcome");
res.render("test");
});
app.listen(3000,function(){
console.log("connected")
});
test.ejs代码:
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
<!-- <link type="text/css" href="app.css">-->
<!-- <link href="/public/app.css" rel="stylesheet" type="text/html">-->
<!-- <link type="text/css" href="/public/app.css">-->
<!-- <link rel="stylesheet" type="text/css" href="/app.css" >-->
<link rel="Style" type="text/css" href="/app.css" >
</head>
<body>
<h1>here</h1>
</body>
CSS 代码:
body{
background-color: green;
}
服务器结构: 根-->公共(public)-->app.css
我还遇到了错误:由于 MIME 类型(“text/html”)不匹配(X-Content-Type-Options:nosniff)而被阻止。但我解决了它,并尝试了很多方法来更改路径并将其与 ejs 保存在同一文件夹中 预先感谢您
最佳答案
在你的 app.js 中
app.use(express.static(__dirname + '/public'));
app.use('/public', express.static(__dirname + '/public'));
关于css - 使用express和node js添加css,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59827547/