简体   繁体   中英

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

I'm trying to serve images from different urls eg: firebase storage, github, imgbb etc... but its not working!

let me explain... here I'm stored img urls on mongodb

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

I've queried data perfectly.... only img not displaying!

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

but local images working.. from static folder

<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
}

Have you try without quotation mark ("")?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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