My Node Js server is exposing an API to build and download an APK file.
Expected behaviour:-
When the user open the API url in browser, I want to show him/her APK file building progress/status. On complete, Download the APK file.
Error:- I cannot call res.download(fileURL) after calling res.write('APK building Progress')
My code:-
function createPersonalAPKAndDownload(req, res) {
const email = req.query.email || req.body.email
if (!email) return res.send('Please enter your email address.')
res.setHeader('Content-Type', 'text/html')
res.write('Encrypting email and password ...<br>')
// Some work
res.write('Work progress or error')
// Some work 2
res.write('Work 2 progress or error')
// finally download the generated APK or write error
if (some_error) {
res.write('some_error')
return res.end()
}
res.download('path/to/apk/file')
}
This code will throw error at res.download -> Error: Can't set headers after they are sent. at SendStream.headersAlreadySent
How can I make it work?