简体   繁体   中英

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 ?

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

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

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

With this path you can display image on front end using img tag

<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'));

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

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