I have tar.gz file on a nodejs server. I want it to be downloadable from another nodejs app. I can do it with txt, jpeg etc... But no success with tar.gz. When I download the tar.gz file seems empty on the client.
This is the simplified basic server that I used
var http = require('http');
http.createServer(function(request, response){
fs.readFile(__dirname + "/update/test.tar.gz", (err, data) => {
if (err) {
} else {
response.setHeader('Content-Type', 'application/x-gzip');
response.end(data);
}
});
This is the client that tries to download
var postData = {};
var options = {
protocol : "http:",
host: "localhost",
port: 3010,
path: '/GetFile',
method: 'POST',
headers: {
'Content-Type': 'application/octet-stream',
'Content-Length': Buffer.byteLength(postData)
}
};
var req = http.request(options, function(res) {
var body = '';
res.on('data', function (chunk) {
body += chunk;
});
res.on('end', function(){
fs.writeFile("./test4.tar.gz", body, function(err) {
});
});
});
req.write(postData);
req.end();
It seems saving file successfully but I cant extract it.
I solved it by creating stream as below.
var req = http.request(options, function(res) {
var body = '';
var file = fs.createWriteStream('./test4.tar.gz');
res.on('data', function(chunk){
file.write(chunk);
}).on('end', function(){
file.end();
});
});
req.write(postData);
req.end();