How to display an image stored in postgres as bytea to client through Nodejs/Express

56 views Asked by At

Im trying to display an image that already save in postgres in a column bytea type... and i try everything, modify server or modify client and nothing get results.

What is the right way to retrieve data from postgres and send it to client using nodejs/express?

This is my server code: (doesnt matter if i use res.send or res.end or if i transform the image to base64 string) server code

solutions that i tried:

 static async getImagen(req, res) {
        const nombre = req.params.nombre
        try{
            const result = await ImagenModel.getImagen()
            if (result.length > 0){
                res.contentType('image/png')
                res.send(result[0].imagen)   
                return
            }

            res.status(400).json({message: "No hay registros"})
        }catch(e){
            console.log(e)
            res.status(500).json({message: "Algo ha salido mal"})
        }
    }

im always get the same response: response in client

the same problem for fetch api (nothing happen, not changes) fetch api

const img = document.getElementById("img")
const uri = 'http://localhost:3000/imagen/imagen'

fetch(uri).then(res => res.blob())
.then(data => {
    img.src = URL.createObjectURL(data)
})

image type display image on client

0

There are 0 answers