node.js:数据库查询+多部分表单数据锁定

|| 我对整个节点事物仍然相当陌生,所以如果这非常愚蠢,请原谅我,但是: 如果我在node-mysql客户端对象上调用“ 0”,则无法再从Web应用程序(快速)中获取请求对象的主体。 我需要用户验证的基本POST / PUT模式请求是:
_db_client = connect_to_db_server();
app.post(\"/some/path\", function (req, res) {
    _db_client.query(\"SELECT * FROM Users WHERE username = ?\",
                     [ http_basic_auth.username ], function (err, results) {
      if (err != null || resp == null) {
         res.send(\"AUTH FAILURE: sorry, you can\'t do that\", 403);
      } else {
         get_request_body(req, function (body) {
            do_something();
            res.send(\"yay, I\'m happy\", 200);
         });
      }
   });
});
现在,问题在于get_request_body函数根本再也不会回来。似乎在请求对象上只是“被阻止”。 这是一个示例程序:
var express = require(\'express\'),
    mysql = require(\'mysql\');

var MySQLClient = mysql.Client;
var client = new MySQLClient();

client.user = \'root\';
client.password = \'\';

client.connect(function(err, res) {
    client.query(\"USE Gazelle\", function (qerr, qresp) {
    });
});

var app = express.createServer(  );
app.use(express.bodyParser());

app.post(\"/test\", function (req, res) {
    client.query(\'SELECT * FROM Users\', function (err, results) {
        get_request_body(req, function (body) {
            console.log(\"wooo\");
            console.log(body);
            res.send(\"cool\", 200);
        });
    });
});


app.listen(8080);


function get_request_body (req, callback) {
    if (req.rawBody != undefined) {
        callback(req.rawBody);
    } else {
        var data = \'\';
        req.setEncoding(\'binary\');

        req.on(\'data\', function(chunk) {
            data += chunk;
        });

        req.on(\'end\', function() {
            callback(data);
        });
    }
}
诀窍:仅当我使用多部分表单POST数据而不是常规POST数据时,它才会失败:
curl -u un:pw -X POST -d \' { \"cat\" : \"meow\" }\' http://localhost:8080/test
作品
curl -u un:pw -X POST -F \"image_file=@test.jpg\" http://localhost:8080/test
锁定并永远不会返回。我已经看到了connect-form和multipart-js的相同错误。现在有点难住了。 我是个白痴吗?这不是正确的做事方式吗?     
已邀请:
        我认为当您将
end
侦听器添加到请求对象时,, 5ѭ事件已经触发。您需要立即在主
app.post
函数作用域中将IO事件侦听器添加到
req
对象中,而不是在数据库查询返回时在异步回调中添加。这是带有一些日志语句的版本,该日志语句显示导致此问题的事件顺序。底部的ѭ9只是用来证明为什么请求被锁定的黑客。您的
end
事件处理程序永远不会被调用,但是合成一个重复的
end
事件确实可以使请求完成(但正文尚未正确解析)。
var express = require(\'express\');
var app = express.createServer(  );
app.use(express.bodyParser());

app.post(\"/test\", function (req, res) {
  req.on(\'data\', function(){
    console.log(\"data fired but your listener is not yet added\");
  });
  req.on(\'end\', function() {
    console.log(\'end fired but your listener is not yet added\');
  });
  setTimeout(function() { //Simulating your async DB query
    console.log(\"setTimeout finished and fired callback\");
        get_request_body(req, function (body) {
            console.log(\"wooo\");
            console.log(body);
            res.send(\"cool\", 200);
        });
      }, 50);
});


app.listen(8081);


function get_request_body (req, callback) {
    if (req.rawBody != undefined) {
        console.log(\'express.bodyParser parsed it already\');
        callback(req.rawBody);
    } else {
        console.log(\'get_request_body going to parse because \' + req.rawBody);
        var data = \'\';
        req.setEncoding(\'binary\');

        req.on(\'data\', function(chunk) {
            console.log(\'get_request_body got data event\');
            data += chunk;
        });

        req.on(\'end\', function() {
            console.log(\'get_request_body parsed it\');
            callback(data);
        });
        req.emit(\'end\');//BUGBUG
    }
}
    

要回复问题请先登录注册