创建 Node.js/Express/EJS 等应用程序...在我的视图目录中,我有我的 home.ejs 和一个相同的 index.ejs 。为什么我的 home.ejs 不显示 css?

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

这给我带来了很多痛苦。我发现我遇到的问题是我的

home.ejs
文件,路线设置为
'/'
。该页面无需任何 CSS 即可显示。

我使用 (

'/'
) 正斜杠 get 路由路由到哪个 EJS 视图并不重要...该视图不会显示正确编码到
<head>
中的 CSS,该 CSS 正确地包含在部分中并包含在内。

试图找到问题,我在视图目录中创建了一个

index.ejs
,复制并粘贴了
home.ejs
中的所有代码,因此它们是相同的。我为
'/index'
设置了路线
index.ejs
。现在,当我加载我的
http://localhost:3000/
时,我看到
home.ejs
没有 css。

http://localhost:3000/index
虽然给了我我想要的CSS和页面。这是怎么回事?

这是我的代码的图像:

  • My Proj Directory
  • My 'index.js' (JavaScript) file
  • 'home and index 'ejs' files side-by-side

这是我第一次提出问题。抱歉,如果我的问题组织得很糟糕...提前感谢您的帮助

总结一下问题所在...如果我尝试对任何 .ejs 文件使用基本

get('/')
路由,则不会显示 css

编辑:这是我的链接代码:

  <!-- Link Google Font Stylesheets-->
  <link rel="preconnect" href="https://fonts.googleapis.com">
  <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
  <link href="https://fonts.googleapis.com/css2?family=Fjalla+One&display=swap" rel="stylesheet">
    
  <link href="//maxcdn.bootstrapcdn.com/bootstrap/4.1.1/css/bootstrap.min.css" rel="stylesheet" id="bootstrap-css">

  <!-- Link Bootstrap CSS-->
  <link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-9ndCyUaIbzAi2FUVXJi0CjmCapSmO7SnpJef0486qhLnuZ2cdeRhO02iuK6FUUVM" crossorigin="anonymous">
  <!-- Link Custom CSS-->
  <link rel="stylesheet" type="text/css" href="/css/styles.css"> 
</head>
css node.js express ejs
1个回答
0
投票

我想我看到了你的问题。

在公开场合,您已包含文件“index.html”

当您加载

localhost:3000
时,该文件及其CSS(或缺少的CSS)将被加载。任何其他路线都会加载您的 ejs 视图。

所以看起来也许

index.html
可能有一些样式需要复制到你的部分?

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