简体   繁体   English

express.static() 无法获取图像

[英]express.static() won't work to fetch images

I am new at programming, sorry if the question is stupid.我是编程新手,如果问题很愚蠢,我很抱歉。 I could not find anything to help me online.我在网上找不到任何帮助我的东西。

On Angular, I have the URL of the image to fetch from the server side.在 Angular 上,我有要从服务器端获取的图像的 URL。 If the URL is如果网址是

http://localhost:3000/h.jpg

and I have我有

app.use(express.static('backend/adImages'));

everything works fine.一切正常。 If I change the path to如果我将路径更改为

http://localhost:3000/adImages/e.jpg

and the code to和代码

app.use('/adImages', express.static('backend/adImages'));

I cannot visualize the image.我无法想象图像。

my path is我的路是

C:\Users\PupoZz\Desktop\mean\personal\eTrex\backend\adImages

is my path.是我的道路。 The server.js file is in the eTrex folder server.js 文件位于 eTrex 文件夹中

another thing.另一件事。 If I run the server to show the database.如果我运行服务器来显示数据库。 I can click the link and open the image:我可以点击链接并打开图片:

_id "5e80643f1bf2d90d685e3e66"
title   "e"
description "e"
location    "e"
postedBy    "null"
image   "http://localhost:3000/adImages/e.jpg"
__v 0

on my inspector I can see在我的检查员身上,我可以看到

"<img _ngcontent-vlp-c2=\"\" class=\"card-img-top\" src=\"http://localhost:3000/adimages/e.jpg\" style=\"display: none !important;\">"

any idea?任何的想法?

thanks Marco谢谢马可

try this:尝试这个:

const path = require('path');
//
app.use(express.static(path.join(__dirname, 'backend', 'adImages'); 

if the file is in the backend folder, exclude the backend如果文件在后端文件夹中,则排除后端

const path = require('path');
//
app.use(express.static(path.join(__dirname, 'adImages'); 

1) 1)

-server.js
-/backend
---/adImages

2) 2)

-/backend
---server.js
---/adImages

try this:尝试这个:

app.use(express.static('backend'));

And more importantly don't forget to restart your node.js server after the fact.更重要的是不要忘记事后重启你的 node.js 服务器。

Happy coding!快乐编码!

thanks to you guys, I checked the right place.谢谢你们,我检查了正确的地方。 It seems that firefox blocked the image because it was in the adimages folder. Firefox 似乎阻止了该图像,因为它位于 adimages 文件夹中。 I just changed the folder name to "images"!我只是将文件夹名称更改为“图像”!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM