繁体   English   中英

express js:如何显示来自静态/公共目录之外的图像

[英]express js: how to display images from outside of static/public dir

我正在尝试提供来自不同网址的图像,例如:firebase 存储、github、imgbb 等……但它不起作用!

让我解释一下...这里我在 mongodb 上存储了 img url

.....
profile_img: "https://avatars.githubusercontent.com/u/51321911?v=4"
cover_img: "https://pbs.twimg.com/profile_banners/820249925384630273/1600164235/60…"
.....

我已经完美地查询了数据......只有img没有显示!

<img src="{{ userData.profile_img }}" //not work

但本地图像工作..来自静态文件夹

<img src="/static/resource/default_avatar.png" //working
//controller.js

const User = require("../../models/User");

const profile = async (req, res) => {
    const otherUsers = await User.find({_id: {$ne: res.locals.user.id}});
    const userData = await User.findOne({username: req.params.username}).select("-password");
    console.log(userData);
    res.render("user/profile.html", {otherUsers, userData});
}

module.exports = {
    profile,
};
userData - output----
{
  _id: new ObjectId("62b702ba4502ee54e96dba64"),
  fname: 'Muhammad',
  surname: 'ABir',
  username: 'muhammadabir',
  email: 'abir@gmail.com',
  profile_img: 'https://avatars.githubusercontent.com/u/51321911?v=4',
  cover_img: 'https://pbs.twimg.com/profile_banners/820249925384630273/1600164235/600x200',
  is_active: true,
  star_badge: true,
  joined: 'Jun 25, 2022',
  friends: [],
  __v: 0
}

你试过不带引号(“”)吗?

<img src= {{ userData.profile_img }}
// or 
<img src= { userData.profile_img }

暂无
暂无

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

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