I Have been building a server which is gonna give out a zip file to download. It does not download the first time. But the second time it downloads. I use jimp to edit images and output those files into a specific directory.
app.use(
'/api/edit',
upload.single('image-file'),
async (req, _res, next) => {
const filePath = path.resolve(process.cwd(), 'user-data', req.file.filename);
// resize every images
await resizeAll(filePath);
// continue request
next();
}
);
app.post('/api/allResize', upload.single('image-file'), (_req, res) => {
// use res.setHeader to set the application file type to zip
setHeaderAsZip(res);
const zip = archiver('zip');
zip.pipe(res);
zip.directory('./user-data/icon-set-imagable', 'icon-set-imagable').finalize();
res.on('close', () => {
cleanUpIconData();
});
});
The first time it generates the resizeAll output it sends a empty zip file but the second time it sends the file but do the cleanUpIconData, which clears out all data generated in the server.