【问题标题】:Css not working in express with ejs template engineCss 不能与 ejs 模板引擎一起工作
【发布时间】:2019-12-26 10:08:15
【问题描述】:

我正在使用带有 ejs 模板引擎的 express js 这是我的文件夹结构

- public
----- css
---------- style.css
----- js
---------- script.js
- views
----- partials
---------- footer.ejs
---------- head.ejs
---------- header.ejs
----- pages
---------- index.ejs
---------- about.ejs
- package.json
- server.js

现在我想在“partials/head.js”文件中添加样式 css,我该怎么做?我尝试使用以下代码,但 不为我工作,我哪里错了?

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

【问题讨论】:

  • should 工作.. 如果您检查浏览器的开发人员工具,浏览器尝试加载的 URL 是什么?您收到 404 错误吗?
  • @cloned: 我收到以下错误 Error
    Cannot GET /public/css/bootstrap.css
  • 它在你的问题中根本没有提到public/css/bootstrap.css

标签: css node.js express ejs template-engine


【解决方案1】:
<link href="/css/style.css" rel="stylesheet" type="text/css">

这是您的答案,因为在 app.js 中您已经定义了用于提供静态文件的公共目录。喜欢css/js/image

app.use(express.static(__dirname + '/public'));

【讨论】:

  • 试试这个,这是唯一的解决方案 否则 并且不要忘记再次运行 npm start 以及让我知道
  • 您是否使用 npm install bootstrap 安装了引导程序?
  • css里面有没有添加bootstrap.css文件?
  • 是的,css文件夹里面有bootstrap.css文件
  • 在 app.js 中添加这一行 app.use(express.static(__dirname + '/public'));
【解决方案2】:

您尝试了以下我认为为您工作的代码。

在链接中将 href 指定为 ../../public/css/style.css

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-10-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-17
    • 1970-01-01
    • 2014-12-19
    • 1970-01-01
    相关资源
    最近更新 更多