title: node.js - Upload image to MySQL table
body: <p>Hello I am having issues displaying images uploaded to MySQL. My table looks like so.</p>
  
  <pre><code>  CREATE TABLE IF NOT EXISTS images(id int primary key auto_increment,
    image_id varchar(255) NOT NULL, img LONGBLOB NOT NULL);
  </code></pre>
  
  <p>I handle the uploaded image on my server like so</p>
  
  <pre><code> const multer = require('multer');
   const upload = multer({ dest: `static/images/product_images/` });       
  
   app.post('/upload-image', upload.single('img'), (req, res) =&gt; {
    const img = req.file; 
  
    fs.readFile(img.path, (err, data) =&gt; {
     connection.query(`
            INSERT INTO product_images(product_id, img)
                      VALUES(?,?)`,
                      [
                          randomId(),
                          data
                      ],
                      (err, results) =&gt; {
                        //etc...
            });
        });
       });
  </code></pre>
  
  <p>Now if I fetch that image it looks like this</p>
  
  <pre><code> "img": {
    "type": "Buffer",
    "data": [
      255,
      216,
      255,
      224,
      0,
      16,
      74,
      70,
      etc..
       ]
      }
  </code></pre>
  
  <p>Am I doing this correctly? When I fetch that image and try to display it on the front end using</p>
  
  <pre><code> &lt;img src='img-blob'/&gt; 
  </code></pre>
  
  <p>It doesn't display the image. What am I doing wrong? </p>
  
  <p>I know uploading images to the db is considered a bad idea by some but I have no other choice </p>