CSS 由于某种原因无法加载到 hbs 文件中

问题描述 投票:0回答:2

所以我有一个非常基本的文件结构,但无论出于何种原因,当我在本地主机上运行它时,我无法让 css 显示在项目中。除了 css 加载之外的其他所有内容都是结构

enter image description here

我已经尝试了各种路径文件,并且只是直接将整个文件路径复制到相关部分,但这仍然不起作用。我正在调用 index.js 文件中的所有内容。

这是代码

const path = require('path');
const express = require('express');
const hbs = require('hbs')
const app = express();
const config = require('./config/request');


const publicDirectoryPath = path.join(__dirname, 'public')
// it was ../public but that did nothing
// also ./public/
// also ../public/
// also /public/
// also public/

const viewsPath = path.join(__dirname, './templates/views')
const partialsPath = path.join(__dirname, './templates/partials')

// error handler
app.use(function(req, res, next) {
    if(config.MARKETPLACE_ID === '' || config.SECRET === '') {
        res.send('MarketplaceId and secret must be set with your marketplace API credentials');
    } else{
        next();
    }
});

app.set('view engine', 'hbs')
app.set('views', viewsPath)
hbs.registerPartials(partialsPath)

app.use(express.static(publicDirectoryPath))

app.get('', (req, res) => {
    res.render('index')
})

app.listen(process.env.PORT || 3000, function(err, req, res) {
    if(err) {
        res.send('There was no endpoint listening');
    } else{
        console.log('server started on port: ', (process.env.PORT || 3000));
    }
});

css 文件(它非常非常复杂,所以请花点时间阅读它)

.main-content {
    background-color: purple;
}

index.hbs 文件

<!DOCTYPE html>

<html>

<head>
    <title>marketplace</title>
    <link rel="stylesheet" src="/literally/the/file/path/I copied/from visual studio code/public/css/styles.css" >
 I even put the file into the terminal to get the exact address cause I was convinced I was spelling something wrong
 unless all the possible tools used to determine file path on my Mac are wrong then this is the correct file path. 
</head>

<body>
  <div class="main-content">

所以是的。 index.hbs 页面应具有紫色背景。它曾经说过由于 MIME 类型或其他原因加载 css 文件时出现错误,但我基本上已经尝试过它并让它消失了。现在没有背景了。没有加载CSS。控制台中没有任何关于错误或文件未加载的信息。那么什么给出了?

有一次我尝试将所有这些加载到我的 CSS 中

<link rel="stylesheet" type="text/css" src="actual path copied from the terminal the path is 100% correct>
<link rel="stylesheet type="text/css" href="100% correct file path">
html css node.js express handlebars.js
2个回答
0
投票

我有同样的问题,所以解决它你应该只在链接src中放入“/css/styles.css”。

<link rel="stylesheet" href="/css/style.css">

我希望它也适合你。


0
投票

你能解决这个问题吗?因为唯一给我 css 到文件的是使用样式标签添加内部 css...如果你解决了这个问题帮助我解决这个...

© www.soinside.com 2019 - 2024. All rights reserved.