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

我正在做以下事情:

...... .... 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("...")被称为(没有任何控制台logging…) – 我在做什么错了?

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 MD5validation

结果是因为我没有在callback的任何地方回复客户,例如:

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

这很奇怪,因为我的callback中的代码执行得很好(即使没有回复客户端),但显然如果节点parsing器意识到有一个req.on()并且没有回应客户端,无论是在内部还是外部,将会忽略req.on(..) ,这是毫无意义的…如果对客户端的回复丢失了,那么整个事情都应该中断或者没有中断。