为什么 ejs 中的某些图像会加载到浏览器中而其他图像却不加载?这可能是什么原因?

What might be the reason why some images in ejs get loaded on the browser why others do not?

cssjs 和图像目录在public 目录中。反过来,public 目录在视图中(我不知道在视图目录中添加 public 目录是否是一个好习惯)。 css 文件和 js 文件(在 public 目录中)正在工作,但只有一个图像(即 navbar 部分的徽标)被加载每当我启动服务器时,在浏览器上,其余图像会在相应位置显示小图标。所有图像都在同一个目录中,我 link 以相同的方式编辑了所有图像。为什么浏览器上只出现一个? 我不知道在 views 目录中添加 public 目录是否是一个好习惯,但是当我将两者添加到与 app.js 相同的路径时,public 中的所有文件都停止工作.但我知道我走错了路,原因如下:

  1. 没有观察到差异 app.use(express.static("public"); 添加到 app.js``` (that is, the server file) or not. This means that that line of code is not working, still the cssandjs` 文件在两种情况下都有效。

  2. 只有一张图像显示在浏览器上,即使所有图像都以相同的方式linked。

  3. 单词 public 出现在每个静态文件的源中,省略它会使所有静态文件停止工作。

应用结构: 医学, webapp, 主要的, 观点, 偏音 footer.ejs header.ejs public bootstrap css js 图片 home.ejs post.ejs news.ejs about.ejs contact.ejs app.js package.json

我希望缩进在这里有所帮助,因为我还没有被允许 post 图片。 home.ejs中的图像是 img src="../views/public/images/syringe-pill-capsule.jpg, img src="../views/public/images/dna-1811955_1920.jpg", img src="../views/public/images/lab-217043_1280.jpg". 唯一在浏览器上加载的是 header.ejs 中导航栏部分的徽标。 link 是 img src="../public/images/wd.jpg"


问题是您没有将正确的路径传递给 express.static。您需要将根更改为实际根,即:

app.use(express.static("./path-to-views/public")

然后,您需要确保在 html 中使用正确的路径,这很简单:

src="/images/syringe-pill-capsule.jpg"


编辑:

您使用的 express.static 的根是正确的,即 app.use(express.static("views")

但是你需要将图片src属性中的路径调整为以下绝对路径: src="/public/images/syringe-pill-capsule.jpg"