I am creating a desktop application using angular2 and electron. This client needs to download zip files from a server using the request library.
To be able to download files is only the first step, The client application will also need to be able to push zip files to the server. Compared how easy this is using the examples below I dont understand what is blocking this.
To get the fs and path library to work in electron I had to do some workaround in the index.html. this workaround is currently working and I am able to create folders within my file system with fs:
<script>
window.fs = require("fs");
window.path = require("path");
</script>
Code
Server
const restify = require('restify');
const server = restify.createServer();
const fs = require('fs');
server.post('/download', function(req, res) {
fs.createReadStream("C:\\test\\test.zip").pipe(res);
});
server.listen(1338, function() {
console.log('server started up on port 1338');
});
Working Client
let request = require("request");
let path = require("path");
let fs = require("fs");
request.post("http://localhost:1338/download").on("response", (response) => {
let zipLocation = path.join("/test", "response.zip");
let writeStream = fs.createWriteStream(zipLocation);
writeStream.on("finish", () => {
console.log("finished");
}).on("error", (err) => {
console.log("err: ", err.message);
});
response.pipe(writeStream);
}).on("error", (err) => {
console.log("req err: ", err.message);
});
But when I introduce this client code into the angular2 electron app. I get a error:
err: Invalid non-string/buffer chunk
I have the request library included within my angular app as such:
import * as request from 'request';
This issue might be connected to the request library. in the response from there are no headers. But I am unable to say for sure
response.headers // is a empty object: {}
Can anyone tell me the reason for as why I am unable to stream files from the server to the file system running the electron app?
UPDATE:After trying to implement file upload I got the same error:
Invalid non-string/buffer chunk