fs.createReadStream()
pipe()
const express = require('express')
const fs = require('fs')
const port = process.env.PORT || 1337
const zipper = require('zip-local')
const app = express()
app.post('/checkFunc', (req, res) => {
// If you're going to use synchronous code then you have to wrap it
// in a try/catch if you don't want to crash your server. In this case
// I'm just handling the error and returning back a 500 error with a
// standard response message of Internal Server Error. You could do more
//
// Using the Error Handling Middleware (err, req, res, next) => {}
// you can create a more robust error handling setup.
// You can read more about that in the ExpressJS documentation.
try {
zipper.sync.zip("./finalFiles").compress().save("pack.zip");
} catch (err) {
console.error('An error occurred zipping', err)
return res.sendStatus(500)
}
// Create a readable stream that we can pipe to the response object
let readStream = fs.createReadStream('./finalFiles/pack.zip')
// When everything has been read from the stream, end the response
readStream.on('close', () => res.end())
// Pipe the contents of the readStream directly to the response
readStream.pipe(res)
});
app.listen(port, () => console.log(`Listening on ${port}`))