Nodejs有什么问题?

我正在使用Muller来处理上传图片。

app.use(multer({ dest: './public/photos', rename: function (fieldname, filename) { return filename+Date.now(); }, onFileUploadStart: function (file) { console.log(file.originalname + ' is starting ...') }, onFileUploadComplete: function (file) { console.log(file.fieldname + ' uploaded to ' + file.path) //a='asass'; done=true; } })); app.post('/api/photo',function(req,res){ if(done==true){ //ERROR here if I remove comment on 'a=asass' >> 'done' is not defined console.log(req.files); console.log(req.body); res.end("File uploaded."); } }); 

我不会在任何地方声明“完成”variables,为什么这个代码仍然有效? 如果我删除“a = asass”的评论,我会在上面finderr。 (我不会在任何地方声明“a”variables)。 当我给variables赋值的时候,我会得到这个错误,但是不会发生。

What done=true正在做的是声明一个名为done的全局variables。 为了使它不是一个全局variables,使用var关键字如var done = true 。 声明全局variables通常被认为是一个坏主意。 JavaScript有一个称为strict mode的可选模式,可以防止您无意中声明全局variableshttps://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Strict_mode

在任何地方使用strict mode都是标准做法,通常在.js文件的顶部声明。