Using global variable in node.js
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) 开发者_Python百科{
tracker.progress.push(bytesReceived);
})
});
};
});
Why "check" return empty uploads ? Should I use global variable in this case ?
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 <input type="file">
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("<A href='/'>ok</a>");
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:
<form action='/upload' method='POST' enctype='multipart/form-data'>
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 :)
upload
is being set to an empty object({}
) on every request, its always going to be {}
.
Note that the function is called on all requests
Global variable has a special meaning in node.js since all modules has a local scope. The solution would be to declare it outside of the function.
Like this.
var uploads = {};
var server = http.createServer(function(req, res) {
....
I hope this helps
精彩评论