简体   繁体   English

使用Pug / Jade和node.js显示图像

[英]Display image with Pug/Jade and node.js

I want to display an avatar of the logged user in the navbar, but everytime I get error 404 with the URL: http://localhost:3000/uploads/avatars/avatar_test3.jpeg my pug code is: 我想在导航栏中显示登录用户的化身,但每次收到URL时出现错误404: http://localhost:3000/uploads/avatars/avatar_test3.jpeg我的哈巴狗代码是:

ul.nav.navbar-nav.navbar-right
        if user
          li
            a(href='/photos/add') Add photo
          li
            a(href='/users/edit')
              img(src=user.avatar)
          li
            a(href='/users/logout') Logout

user.avatar gives me: uploads/avatars/avatar_test3.jpeg I have also tried with the: user.avatar给我: uploads/avatars/avatar_test3.jpeg我也尝试过使用:

  • img(src='../' + user.avatar)
  • img(src='./' + user.avatar)
  • img(src='/' + user.avatar)

The same error every time 每次相同的错误

The directory hierarchy: 目录层次结构:

/
 -uploads
    -avatars
       -avatar_test3.jpeg
  -views
    -layout/pug //the template I'm using 

I also have tried with move the avatar_test3.jpeg file directly to the view directory but without any success. 我也尝试将avatar_test3.jpeg文件直接移动到视图目录,但没有成功。

How can I resolve it? 我该如何解决?

Which error are you getting? 您遇到什么错误? If it is http 404, you should turn your uploads folder public through express: http://expressjs.com/en/starter/static-files.html 如果是http 404,则应通过express将您的上传文件夹设为公开: http//expressjs.com/en/starter/static-files.html

Something like: 就像是:

app.use('/uploads', express.static('uploads'))

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

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