1

我正在用 node.js 构建一个超级简单的文件服务器。

我正在使用 dotcloud 来托管我的服务器,可以在此处访问。

当我尝试上传文件(使用正确的密码)时,我的代码返回'Error, incorrect password!'. 看看下面的代码,我会解释原因:

var success = false;
        var form = new formidable.IncomingForm();
        form.parse(req, function(err,fields, files) {
            console.log("Password: " + fields.password);
    if(fields.password!="poop") {
        return;
    }
       fs.rename(files.upload.path, files.upload.name, function (err) {  });
       success = true;
          res.writeHead(200, {'content-type': 'text/plain'});
          res.write('received upload:\n\n');
          res.end();
        });
        req.on('end', function() {
          // empty 200 OK response for now
      if(success) {
              res.writeHead(302, { 'location': '/' });
          } else {
      res.writeHead(200, {'content-type': 'text/plain'});
              res.write('Error, incorrect password!\n');
      }
          res.end();
        });

我认为我的代码在“fs.rename(files.....)”处失败,因为成功变量未设置为 true。我的代码适用于我的本地 node.js 安装,我是否需要从 dotcloud 请求写入权限或其他什么?

4

1 回答 1

0

我相信endHTTP 请求的事件是在强大解析表单之前发出的。

Node.js 文档中没有定义事件调度的确切顺序;因此,根据 Node.js 版本和/或平台,它们最终可能会以不同的顺序进行处理。

在这种情况下,如果发生这种情况,success那么false您甚至在实际解析表单之前发送回复。

也许您应该在表单解析函数中处理错误?

于 2012-05-18T23:10:03.483 回答