繁体   English   中英

Node.js-即使req.body不为空,也不会调用POST中的req.on(“ data”)

[英]Node.js - req.on(“data”) in POST is never called even though req.body is not empty

我正在执行以下操作:

......
....
app.use(cookieParser());
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({  
  extended: true 
})); 

app.use('/public', express.static(__dirname + '/public'));

app.post('/uninstalled', function (req, res, next)
{     
  var bodyAsQueryString = queryString.stringify(req.body);
  console.log('bodyAsQueryString = ' + bodyAsQueryString);

  var hmacReceived = req.headers['x-shopify-hmac-sha256'];
  var calculatedHmac = crypto.createHmac('SHA256', config.app.secret);

  req.on("data", function(data) 
  {
     console.log('on data...');
     calculatedHmac.update(data);
  });

  req.on("end", function() 
  {
     console.log('on end...');
     calculatedHmac = calculatedHmac.digest("base64");
     console.log('hmacReceived = ' + hmacReceived);
     console.log('calculatedHmac = ' + calculatedHmac);
  });

  req.on("error", function(err) 
  {
     console.log('on error...');
     console.log(err);
     return next(err);
  });
});

上面的req.on("...")都没有被调用过(控制台记录没有...)-我做错了什么?

bodyAsQueryString的值始终看起来像以下内容(我已经用xxxxx替换了个人数据):

ID = XXXXX&名称= XXXXX和电子邮件= XXXXXX&域= XXXXXXX&created_at = 2015-03-21T00%3A31%3A36%2B00%3A00&全省=国家= GB&地址1 = XXXXXXXXX及邮编= E59JY与城市=伦敦和源= XXXX和手机= XXXXXX&的updated_at = 2015-08-19T15%3A12%3A31%2B01% 3A00&CUSTOMER_EMAIL =纬度= XXXXX和经度= -xxxxxx&primary_location_id = primary_locale = EN&COUNTRY_CODE = GB&COUNTRY_NAME =联合%20Kingdom与货币= USD和时区=(GMT%2B00%3A00)%20Europe%2FLondon&iana_timezone =欧洲%2FLondon&shop_owner = XXXXXX&money_format =%24%20%7B%7Bamount%7D% 7D&money_with_currency_format =%24%20%7B%7Bamount%7D%7D%20USD&PROVINCE_CODE =&taxes_included =假tax_shipping =&county_taxes =真plan_display_name =联盟&PLAN_NAME =联盟&myshopify_domain = xxxxxx.myshopify.com&google_apps_domain =&google_apps_login_enabled =&money_in_emails_format =%24%7B%7Bamount%7D%7D&money_with_currency_in_emails_format = %24%7B%7Bamount%7D%7D%20USD&eligible_for_payments =假requires_extra_payments_agreement =假password_enabled =真has_storefront =真setup_required =假

您需要使用原始POST正文而不是req.body。

尝试这样的事情:

app.post('/somepath', function (req, res, next) {
    var hmacReceived = req.headers['x-shopify-hmac-sha256'];
    hmac = crypto.createHmac('SHA256',secret);

    req.on("data", function(data) {
        hmac.update(data);
    });

    req.on("end", function() {
        var calculatedHmac = hmac.digest("base64");
        var test1 = hmacReceived === calculatedHmac;
    });

    req.on("error", function(err) {
        return next(err);
    });
}

此类似问题的更多详细信息:

使用Node.js,Express和Trialpay进行HMAC MD5验证

原来是因为我没有在回调中的任何地方回复客户端,例如:

res.sendStatus(200); or res.sendStatus(500); etc...

这很奇怪,因为我的回调req.on()的代码执行得很好(即使没有响应客户端),但是很显然,如果节点解析器意识到存在req.on()且没有响应客户端,无论其内部还是外部,它将忽略req.on(..) ,这实际上没有任何意义。如果丢失了对客户的回复,则整个事情应该中断,或者全部中断。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM