Node.js服务器上的HTML网站不显示图像

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

所以我对此很新,但我认为应该可以解决这个问题。

我使用Raspberry Pi 3和Node.js来创建一个控制GPIO的网站。我用HTML写了这个网站。

现在我想要一个图像作为网站上的开关背后的背景。如果我通过网址链接图像(例如谷歌的图片),这没问题。

但如果我链接Pi上的图片,它就不会在网站上显示。它显示文件的名称但不显示它。我把图像放在与html文件相同的文件夹中。

这是我做的html代码:

<html>
<head>
<body>

<h1>Control LED light</h1>

<img src="Winkraftanlage.png" alt="Windkraftanlage">
</body>
</head>

<body>
<input id="light" type="checkbox">LED
</body>

```</html>


Thanks a lot in advance.
html node.js
1个回答
0
投票

为什么不使用express来处理所有静态文件并使用ejs模板处理前端import express.js到你的app.js

var express = require('express');
var app = express();
Now let node know the path of the public folder :
var publicDir = require('path').join(__dirname,'/public');
app.use(express.static(publicDir));

__dirname为您提供当前目录(在本例中为app.js所在的根目录)

现在,每当访问这样的URL时,节点将提供来自上述目录的静态文件,例如。

http://localhost/myapp/public/imgs/myImage.jpg

只需将适当的URL放在前端的src中,如下所示:

将index.html重命名为index.ejs

在你的app.js

app.set('view engine', 'ejs');

最后运行npm install

并粘贴你的图像:

<img src="public/imgs/Winkraftanlage.png" alt="Windkraftanlage"">
© www.soinside.com 2019 - 2024. All rights reserved.