The below server code is working fine for 5GB file using
wget http://localhost:11146/base/bigFile.zip
but not using client side code.
Server side code.
var http = require("http");
var fs = require("fs");
var filename = "base/bigFile.zip";
var serv = http.createServer(function (req, res) {
var stat = fs.statSync(filename);
res.writeHeader(200, {"Content-Length": stat.size});
var fReadStream = fs.createReadStream(filename);
fReadStream.on('data', function (chunk) {
if(!res.write(chunk)){
fReadStream.pause();
}
});
fReadStream.on('end', function () {
res.end();
});
res.on("drain", function () {
fReadStream.resume();
});
});
serv.listen(1114);
Client side code using Request module. What is wrong in this code?
var request = require('request')
request('http:/localhost:11146/base/bigFile.zip', function (error, response, body) {
console.log('error:', error); // Print the error if one occurred
console.log('statusCode:', response && response.statusCode); // Print the response status code if a response was received
console.log('body:', body); // Print the HTML for the Google homepage.
});
error for above client side code is below -
error: Error: Invalid protocol
at Request.init (/Users/air/Projects/node_modules/request/request.js:338:51)
at new Request (/Users/air/Projects//node_modules/request/request.js:105:8)
at request (/Users/air/Projects/Vertico/Vertico-CLI/node_modules/request/index.js:53:11)
at Object.<anonymous> (/Users/air/Projects/req.js:2:1)
at Module._compile (module.js:569:30)
at Object.Module._extensions..js (module.js:580:10)
at Module.load (module.js:503:32)
at tryModuleLoad (module.js:466:12)
at Function.Module._load (module.js:458:3)
at Function.Module.runMain (module.js:605:10)
statusCode: undefined
body: undefined
I revised client side to use
wget
shell process instead of request package, that code is below, the problem is - I am not able to see nice download progress of wget, any work around for this code, such that I can see the progress bar in child process.
const fs = require('fs');
const child_process = require('child_process');
var workerProcess = child_process.spawn('wget', ['-O','fdsf.zip', 'http://localhost:11146/base/bigFile.zip']);
workerProcess.stdout.on('data', function (data) {
console.log('stdout: ' + data);
});
workerProcess.stderr.on('data', function (data) {
//console.log('stderr: ' + data);
});
workerProcess.on('close', function (code) {
console.log('Download Completed' + code);
});
So finally I want to know how to download a file using client side code written in nodejs?