How to access Heroku Uploaded Image;

Viewed 283

I successfully writed an image to Heroku, but i can't figure out how to access to it.

function saveImage(req,res) {
  const roomID = req.params.id;

  pool.query('SELECT * FROM ' + roomTable + ' WHERE id = $1', [ roomID ],function(err, result) {
    if(err){
      res.json(
        { "status": err.message }
      )
    } else {
      const selectedRoom = JSON.parse(JSON.stringify(result.rows[0]));
        if(selectedRoom.image_url === null){
          const image = req.body.image_data;
          const fileName = uuid.v4();
          const filePath = __dirname + "/image/" + fileName + ".png" 
          const base64Data = image.replace(/^data:image\/png;base64,/, "");

          fs.existsSync("image") || fs.mkdirSync("image");
          fs.writeFile(filePath,  new Buffer(base64Data, "base64"), function(err) {
            if(err) {
                res.json(
                  { "status": err.message }
                )
            }else{
              pool.query('UPDATE '+ roomTable +' SET image_url = $1 WHERE id = $2;', [filePath, roomID] ,function(err, result) {
              if(err){
                res.json(
                  { "status": err.message }
                )
              } else {
                console.info("The file was saved!");
                res.json({
                  "status": "ok",
                  "path": filePath
                });
              }
              })
            }});
        }else {
          res.json({
            "status": "error",
            "message": "The room has an image already."
          });
        }
    }
  })
}

My server is running on heroku. If i send a request, i get back the right response.

{
    "status": "ok",
    "path": "/app/image/04efd85d-e9db-4965-b15f-3840dbd344f3.png"
}

So, after that the image writed to heroku.

but if i would like to access it, on the url:

https://myurl/app/image/04efd85d-e9db-4965-b15f-3840dbd344f3.png

or, in that url:

https://myurl/image/04efd85d-e9db-4965-b15f-3840dbd344f3.png

i also tried to provide it:

app.use('/image', express.static('image'));
app.use(__dirname + '/image', express.static('image'));

So the question is that, how can i access to my url in heroku after writing it there. I don't want to use Amazon S3.

0 Answers
Related