How to access the downloaded Heroku image;

I have successfully submitted the image to Heroku, but I cannot figure out how to access 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 submit a request, I will return the correct answer.

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

      

So, after that, the image came out to the hero.

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

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

      

or, in this 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 how can I access my url in heroku after writing it. I don't want to use Amazon S3.

+3


source to share





All Articles