Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/366.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript EJS CSS未加载前端_Javascript_Css - Fatal编程技术网

Javascript EJS CSS未加载前端

Javascript 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'));

因此,我为.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'));

// 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')));