I'm getting image from NodeJS server in form of createReadStream.pipe(res). I'm receiving the response using axios. My question is how do I display that response in image tag in react frontend
My code of backend which is sending the data
router.get('/get_profile_image/:filename',(req,res)=>{
gfs.files.findOne({filename:req.params.filename},(err,file)=>{
if(file){
const readStream=gfs.createReadStream(file.filename)
readStream.pipe(res);
}
else{
res.status(403).json("no file with this name")
}
})
})
My frontend code that receives the response:
import React from 'react';
import './friend.css';
import axios from 'axios';
class Friend extends React.Component{
constructor(props){
super(props);
this.state={name:'',picture:[]}
axios.get(`http://localhost:3002/get_name/${this.props.id}`)
.then(res=>{
this.setState({name:res.data})
})
axios.get(`http://localhost:3002/image/get_profile_image/
${this.props.profile_pic_id}`)
.then(res=>{
let picture=new Buffer('binary').toString('base64');
this.setState({picture:picture})
})
}
render(){
console.log(this.state.picture)
return(
<div className='inside'>
<img src={this.state.picture} height='200px' width='200px'/>
<h3>{this.state.name}</h3>
<br/>
</div>
)
**strong text**}
}
export default Friend;
I am getting nothing displayed in my image tag. I am new to this so I'm finding it difficult to understand the already present answers
Thank You.