I've created a file sharing app to use on my home network. It's kind of similar to the web interface for Google Drive or Dropbox but without any file size caps and no security.
It works perfectly when transferring small files instantly from other computers connected via LAN, but something bizarre happened while testing it on a 2GB file.
It took about four hours for the progress bar to get to 50% before I cut it off. Also, take a look at how much memory Node is using.
It will start off low and build its way up to as much as 13GB, then dump and restart, several times a minute.
The file is uploaded from a browser interface using an XMLHttpRequest. This is the essential part of front-end code.
var formData = new FormData();
var file = document.getElementById("fileinput").files[0];
formData.append("file", file);
var xhr = new XMLHttpRequest();
xhr.open("post", "/fileupload", true);
xhr.send(formData);
And on the server side there's a very simple handler using express that takes req.files.file
and passes it directly into fs.writeFile to save it to disk. ('req.files.file' seems to be a Buffer type according to a console.log, which apparently can be piped right onto disk).
var express = require("express");
var fileUpload = require("express-fileupload");
var app = express();
app.use(express.static("public"));
app.use(fileUpload());
var fs = require("fs");
app.post('/fileupload', function(req, res) {
if(req.files && req.files.file){
var file = req.files.file;
fs.writeFile("./public/shared/" + file.name, file.data, "binary", function(err) {
if(err) {
res.send(err);
return;
}
res.send("File uploaded successfully.");
});
}
else{
res.send("No file was uploaded.");
return;
}
});
I'm using express-fileupload to handle uploads.
Both computers are running Windows. The sending computer was using Chrome. Node version is 7.5.0.
Where am I going wrong? How do I approach this problem?