Javascript EJS CSS未加载前端
因此,我为.css文件设置了中间件和静态文件夹,但当我尝试使用它并将其加载到EJS文件中时,我在chrome调试器中收到一个错误,表示在网络下它已被取消 app.jsJavascript EJS CSS未加载前端,javascript,css,Javascript,Css,因此,我为.css文件设置了中间件和静态文件夹,但当我尝试使用它并将其加载到EJS文件中时,我在chrome调试器中收到一个错误,表示在网络下它已被取消 app.js // view engine exapp.set('view engine', 'ejs'); exapp.set('views', path.join(__dirname, 'views')); console.log('Static location: ' + path.join(__dirname, 'views'));
// view engine
exapp.set('view engine', 'ejs');
exapp.set('views', path.join(__dirname, 'views'));
console.log('Static location: ' + path.join(__dirname, 'views'));
// body parser
exapp.use(bodyParser.json());
exapp.use(bodyParser.urlencoded({extended: false}));
// set static path
exapp.use(express.static(path.join(__dirname, 'public')));
console.log('Static location: ' + path.join(__dirname, 'public'));
exapp.use(express.static(path.join(__dirname, 'public/css')));
console.log('Static location: ' + path.join(__dirname, 'public/css'));
// get requests
exapp.get('/', function(request, response){
response.render('index');
})
exapp.get('/about', function(request, response){
response.render('about');
})
header.ejs
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Unicorn</title>
<link rel="stylesheet" href="/public/css/main.css" type="text/css" media="screen" charset="utf-8">
</head>
<body>
我在chrome调试中发现的图片
修好了
出于某种未知的原因,我需要将“/public/css”作为第一个参数添加到use函数中
原创的
exapp.use(express.static(path.join(__dirname, 'public/css')));
固定的
希望这能帮助其他人:
exapp.use('/public/css', express.static(path.join(__dirname, 'public/css')));