[英]How do I Display Images in React which are downloaded as Files & saved in a Folder on the Server with the Filepath stored in my Database?
React.js, Node.js/Express.js & PostgreSQL. React.js、Node.js/Express.js 和 PostgreSQL。
I scrape an ImageUrl of a Book, download and store it in a File in an Image Folder with Node.js and store the Filepath in my postgreSQL Database AND I would like to fetch and display the Image in an <img />
tag on my React frontend.我刮了一本书的ImageUrl,下载并使用Node.js将其存储在图像文件夹中的文件中,并将文件路径存储在我的postgreSQL数据库中,我想在我的React前端的
<img />
标签中获取并显示图像.
The Image I Fetch from my Database through my API doesn't get displayed, eventhough I can console.log the Image Object in the devtool-console and even make a succesful Postman request with the Image-Object as JSON response. The Image I Fetch from my Database through my API doesn't get displayed, eventhough I can console.log the Image Object in the devtool-console and even make a succesful Postman request with the Image-Object as JSON response. Furthermore I can get the image even be displayed if I go to localhost:3001/image-193x278.png.
此外,如果我将 go 转到 localhost:3001/image-193x278.png,我什至可以显示图像。 My
<img />
Tag has even the right Img src attribute displayed in the devtools (images\image-193x278.png)我的
<img />
标签甚至在 devtools (images\image-193x278.png) 中显示了正确的 Img src 属性
In the devtool-console I get the following error: GEThttp://localhost:3002/images/image-193x278.png [HTTP/1.1 404 Not Found 0ms]在 devtool-console 我收到以下错误: GEThttp://localhost:3002/images/image-193x278.png [HTTP/1.1 404 Not Found 0ms]
(Note: My Frontend runs on localhost 3002 while my server & frontend are running concurrently with a proxy on 3001, but that isn't the causing Problem here) (注意:我的前端在 localhost 3002 上运行,而我的服务器和前端与 3001 上的代理同时运行,但这不是导致问题的原因)
const [image, setImage] = useState([]);
useEffect(() => {
async function fetchData() {
const response = await fetch('http://localhost:3001/test', {
method: 'GET',
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
}
})
const data = await response.json();
console.log(data);
setImage(data);
}
fetchData();
}, []);
I tried to get it displayed in two different ways:我试图让它以两种不同的方式显示:
<img className={css(styles.Image)} src={image.test_images} alt="" />
{image.map((img) =>
<img className={css(styles.Image)} src={img.test_images} alt="" />)}
app.get('/test', async (req, res) => {
try {
const image = await testdb.query('SELECT * FROM test_table');
res.json(image.rows);
} catch (err) {
console.error(err);
}
})
I serve an Express Static File我提供 Express Static 文件
app.use(express.static('images'));
I even tried two additional varients just in case我什至尝试了另外两个变种以防万一
app.get('/test', express.static(__dirname + '../images'));
app.use(express.static('../images'));
Still with no succes.仍然没有成功。 I hope i could make myself understood as good as possible and I would be extremely thankful for every help thrown out my way.
我希望我能尽可能地让自己被理解,我会非常感谢每一个帮助我的人。
The problem is with your static folder.问题在于您的 static 文件夹。 The static files will be available at
localhost:3002/:filename
. static 文件将在
localhost:3002/:filename
可用。
If you want to make it work under /images
path, then you can add the path to the static declaration like如果你想让它在
/images
路径下工作,那么你可以将路径添加到 static 声明中,例如
app.use('/images', express.static('images'))
Then you can hit the localhost:3002/images/:filename
.然后你可以点击
localhost:3002/images/:filename
。
You can find the documentation here: https://expressjs.com/en/starter/static-files.html您可以在此处找到文档: https://expressjs.com/en/starter/static-files.html
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.