简体   繁体   English

用Mongoose查询数据时如何显示Multer上传的图像

[英]How to display image which was uploaded by Multer when query data with Mongoose

As title says, I want to display avatar but it has uploads\\\\img.jpg path, how to display it in frontend part of a application for example when you get user info from the database ? 如标题所述,我想显示头像,但是它具有uploads\\\\img.jpg路径,例如,当您从数据库中获取用户信息时,如何在应用程序的前端部分显示它?

Here is the user data: 这是用户数据:

{ 
       username: 'm',
       name: 'm',
       email: 'mmerko@gmail.com',
       id: '5b64247eb03a573b2818f3c4',
       avatar: 'uploads\\img-1533289594220.jpg'
}

So, first of all, if you want to operate with images (or other static files) with you're Node.js server you need to configure static paths 因此,首先,如果要在Node.js服务器上处理图像(或其他静态文件),则需要配置静态路径

Then, let's says you configured it with path /uploads and (for example) you're Node.js server running on localhost:3000 then you can access to any file in this directory by given path 然后,假设您使用/uploads路径对其进行了配置,并且(例如)您是在localhost:3000上运行的Node.js服务器,则可以通过给定路径访问此目录中的任何文件

http://localhost:3000/uploads/example.jpg

With this path you can display image on front end using img tag 通过此路径,您可以使用img标签在前端显示图像

<img src="http://localhost:3000/uploads/example.jpg"/>

Hope it helps. 希望能帮助到你。

Try this Go to your app.js file and do this app.use('/uploads', express.static('uploads')); 试试这个转到您的app.js文件并执行此app.use('/ uploads',express.static('uploads'));

The reason is because by default the folder is not publicly accessible and you don't have a route that handles getting files from that url 原因是因为默认情况下该文件夹不可公开访问,并且您没有用于处理从该url获取文件的路由。

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

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