Express.js 无法使用 express.static 显示目录"images"中的图像



下面是我的index.js文件,我在本地主机上设置了端口3000来为index.html提供服务。

const express = require('express');
const app = express();
const port = 3000;
// trying to make this directory public
app.use(express.static('images'));
app.get('/', (req, res) => res.sendFile(__dirname + '/index.html'));
app.listen(port, () => console.log(`Example app listening on port ${port}!`));

index.html调用img资产,如下所示:

<body>
<h1>hello</h1>
<img src="images/favicon512.png" />
</body>

该图像不会显示在浏览器中。我认为app.use(express.static('images'));将公开目录images中的所有内容。

我的应用程序结构如下:

index.js
index.html
images/

尝试使用:

<img src="/favicon512.png" />

相关内容

最新更新