Javascript 从grunt contrib连接请求对象获取数据

Javascript 从grunt contrib连接请求对象获取数据,javascript,gruntjs,grunt-contrib-connect,Javascript,Gruntjs,Grunt Contrib Connect,所以这可能是一个非常简单的问题,但我已经在谷歌上搜索了一个多小时,没有找到任何东西。我还试着打印出请求对象,但没有看到任何有用的东西 如何在grunt contrib connect中间件定义中获取客户机请求的数据或主体 connect: { main: { options: { hostname: "0.0.0.0", port: 8080, /** * These are the mocked out backends for

所以这可能是一个非常简单的问题,但我已经在谷歌上搜索了一个多小时,没有找到任何东西。我还试着打印出请求对象,但没有看到任何有用的东西

如何在grunt contrib connect中间件定义中获取客户机请求的数据或主体

connect: {
  main: {
    options: {
      hostname: "0.0.0.0",
      port: 8080,
      /**
       *  These are the mocked out backends for various endpoints
       */
      middleware: function(connect, options, middlewares) {
        middlewares.unshift(function(req, res, next) {
          if (req.url !== '/v1/accounts/_findEmail') {
            return next();
          }

          // ********
          // How do I get the data content of the request?
          var data = req.data; // Is undefined
          // ********

          if (data && data.email === 'taken@email.com') {
            res.writeHead(200, {"Content-Type": "application/json"});
            res.write(JSON.stringify({email:"found"}));
          } else {
            res.writeHead(404, {"Content-Type": "application/json"});
            res.write(JSON.stringify({email:"not found"}));
          }

          res.end();
        });

        return middlewares;
      }

    }
  }
}

因此,要使这项工作顺利进行,需要做几件事

如上所述,connect在本例中基本上只是包装nodej,这是我应该猜到的。因此,请求对象实际上是,并且应该以相同的方式使用

因此,代替
var data=req.data我可以添加一个回调,比如
req.on('data',function(data){//do stuff})并以这种方式查看数据

除此之外,在读取数据之前,我必须添加
req.setEncoding('utf8')以使其显示为字符串而不是十六进制数组

因此,最终的解决方案如下所示:

connect: {
  main: {
    options: {
      hostname: "0.0.0.0",
      port: 8080,
      /**
       *  These are the mocked out backends for various endpoints
       */
      middleware: function(connect, options, middlewares) {
        middlewares.unshift(function(req, res, next) {
          if (req.url !== '/v1/accounts/_findEmail') {
            return next();
          }

          req.setEncoding('utf8');
          req.on('data', function (rawData) {
            var data = JSON.parse(rawData);

            if (data && data.email && data.email === 'taken@email.com') {
              res.writeHead(200, {"Content-Type": "application/json"});
              res.write(JSON.stringify({email:"found"}));
            } else {
              res.writeHead(404, {"Content-Type": "application/json"});
              res.write(JSON.stringify({email:"not found"}));
            }

            res.end();
          });
        });

        return middlewares;
      }

    }
  }
}

我无法解释,但@static416的解决方案对我不再有效。所以我想出了一个新的:

首先安装npm包体解析器并导入它。 其次,将其添加到中间件中:

module.exports = function(grunt) {
    var bodyParser = require('body-parser');
    ...
}
现在,您可以在中间件中使用
req.body
来访问数据

middleware: function(connect, options, middlewares) {
          // inject a custom middleware into the array of default 
          middlewares.unshift(
              bodyParser.urlencoded({extended: true}),
              bodyParser.json(),
              function(req, res, next){
                // use data from post request
                console.log(req.body);
                next();
              }, ...
          )
}, ...

谢谢你发布这个。你救了我几个小时!顺便说一句,它对我来说没有
req.setEncoding('utf8')