下面是我的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" />