Store Image file in Binary data in mongoose schema and Display image in html form
<p>I am using Express, Node.js, and Mongodb to create the webpage that uploads and displays the image file. </p>

<p>I saved the binary of image in mongodb using schema.</p>

<p>Here is my little bit of code in index.js and db.js.. </p>

<pre><code>var Post = mongoose.Schema({
   image: {data: Buffer, contentType: String}
});

var post= new Post({ });
post.image.data=fs.readFileSync(req.file.path);
post.image.contentType='image/png';
</code></pre>

<p>and here is the part of mongo shell after I submitted image file and searched for post, and its image field</p>

<pre><code>"image:     {"data:BinData(0,"iVBORw0KGgoAAAANSUhEUE....(I
just cut off the rest of the code.. cuz it was too long)          
rkJggg=="), 
"contentType" : "image/png" } 
</code></pre>

<p>so it seemed like it's successfully saving the binary data of image file in mogngodb, </p>

<p><strong>but my problem is how to display the image on the webpage now using binary data. How do I convert binary buffer data to create image tag??</strong></p>

<pre><code>&lt;img src= "data:{{image.contentType}};base64,{{image.data}}"&gt;
</code></pre>

<p>I tried this, but it gives me an error: </p>

<pre><code>Failed to load resource: net::ERR_INVALID_URL
</code></pre>

<p>Could you guys please let me know how do I solve this??
I will really appreciate for your help :(((</p>