I am building an image compressor using node and react. The server sends the client the image by res.download(filepath)
, but in the client the doesn't happen. But I get the 200 OK response in the network and the image attachment. How can I trigger download in the client automatically?
Here is my the express router that handles image compression and download code:
app.get('/download/:imageName', async (req,res)=>{
let imageName = req.params.imageName
let path = 'uploads/'+imageName;
//Image compression code
const compressedImage = await imagemin([path], {
destination: "output",
plugins: [
imageminPngquant({
quality: [0.6, 0.8]
}),
imageminMozjpeg(),
imageminGifsicle({lossy: 70}),
imageminSvgo({
plugins: extendDefaultPlugins([
{name: 'removeViewBox', active: false}
])
})
]
});
// Here filepath = process.cwd()+"/"+compressedImage[0].destinationPath
res.download(process.cwd()+"/"+compressedImage[0].destinationPath)
})
Here is my client-side function which sends get request for downloading the image:
function downloadAnImage(imageLink){
fetch("/download/"+imageLink)
}
By the way, I didn't handle the response on the client-side because the download should happen after getting the response.
But If the type the full image URL path in the browser the download happens. Then I thought I should send the full image URL to the client but I couldn't find a to get the host URL that my server is running. I know the URL my server is running but Since I am gonna host this app, manually putting "localhost:3001" as the host url is not gonna work, right?