Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Using global variable in node.js

Using global variable in node.js

Problem

I have a simple example:

server = http.createServer(function(req, res) {
    uploads = {}
    if (req.url == '/check') {
        res.writeHead(200, {'content-type': 'text/plain'});
        res.write(JSON.stringify(uploads));
         res.end();
    }
    if (req.url == '/upload') {
        var form = new formidable.IncomingForm();
        form.on('fileBegin', function(field, file) {
            var tracker = {file: file, progress: [], ended: false};
            uploads[file.filename] = tracker;
            file.on('progress', function(bytesReceived) {
                tracker.progress.push(bytesReceived);
            })
        });
    };
});

Why "check" return empty uploads ? Should I use global Variable in this case ?

Problem courtesy of: Bdfy

Solution

Your problem has nothing to do with global variables - when a new variable is created in JavaScript without being prefixed by var, it is dynamically scoped AFAIK, which means it is global. Anyway, I would feel it better if your variable was indeed declared outside the function for not only being global, but for looking global too:

var uploads = {}
server = http.createServer(function(req, res) {
    if (req.url == '/check') {
    // ...

Now, let us see your real problem: you are using formidable in a very strange way. I, for one, have never used the on() method of formidable.IncomingForm. I would recommend you to use the parse() method, which provides you with a key-value object where the keys are the name of the field and the value is the upload file data, such as the path of the file in the server:

if (req.url == '/upload') {
    var form = new formidable.IncomingForm();
    form.parse(req, function (err, fields, files) {
        for (var filename in files) {
          var file = files[filename];
          var tracker = {filename: filename, progress:[], ended:false};
          fs.readFile(file.path, function(err, filecontent) {
            tracker.progress.push(filecontent);
            tracker.ended = true;
          });
          uploads[filename] = tracker;
        }
        res.writeHead(200, {'content-type': 'text/html'});
        res.write("ok");
        res.end();

    });

IncomingForm.parse() will process all the files before call the callback. You will have all the files available, and the path to them. Of course, in this example our tracker.progress will contain only one bunch of data - which will be the complete file content.

Also, note that the form you will use to upload the file should have the enctype='multipart/form-data' attribute:

That is it, hope this helps. Note that I am assuming that you need to solve a problem; if you want to test the IncomingForm.on() method, however, this answer will not be much useful. I hope I have made the correct guess :)

Solution courtesy of: brandizzi

Discussion

View additional discussion.



This post first appeared on Node.js Recipes, please read the originial post: here

Share the post

Using global variable in node.js

×

Subscribe to Node.js Recipes

Get updates delivered right to your inbox!

Thank you for your subscription

×