It would seem a jaded question, but in Express 4 I can't do it at all. I need to get a raw POST request and encrypt it using crypt . How I tried to do this:

 var crypto = require('crypto'); function calculateSignature(key) { return function(req, res, next) { var hash = req.header("TrialPay-HMAC-MD5"), hmac = crypto.createHmac("md5", key); req.on("data", function(data) { console.log("Data is: ", data); hmac.update(data); }); req.on("end", function() { console.log("Called end"); var crypted = hmac.digest("hex"); if(crypted === hash) { // Valid request return res.send("Success!", { "Content-Type": "text/plain" }); } else { // Invalid request return res.send("Invalid TrialPay hash", { "Content-Type": "text/plain" }, 403); } }); req.on("error", function(err) { return next(err); }); } } app.post("/trialpay", calculateSignature("[MY MERCHANT KEY]")); 

Bottom line: - Eternal waiting for an answer, nothing in the console. How to get an analog file_get_contents("php://input"); in Express.JS?

  • Can it be easier to use ready bodyParser.raw() ? It even has a verify option. - Alexey Ten
  • @AlexeyTen, I initialize the module, for example app.use(bodyParser.raw()) , and then how do I get this raw from the request? req.body.toString() or how? - blits
  • Read the documentation. Everything is written there. - Alexey Ten
  • @AlexeyTen, as I understand it, this middleware completely replaces req.body . And then I need to spar him. I tried to use it at the beginning of the script using app.use(bodyParser.raw()); , but nothing has changed at all, as was the type of req.body Object , and remained. - blits
  • In the default express.js, req.body doesn't req.body at all. Apparently you already have some kind of middleware that creates it. I suppose that's why your code doesn't work. - Alexey Ten

1 answer 1

As a result, I went in an unusual way. BodyParser offers a parameter such as verify . With it, you can get a request Buffer and add it to Express.Request . It turned out like this:

 var express = require('express'), router = express.Router(), crypto = require("crypto"), User = require("../../../models/User").User, bodyparser = require("body-parser"); rootApp.use(bodyParser.urlencoded(({verify: (req, res, buf, encoding) => { req.bodyRaw = buf; }}))); function calculateSignature(key) { return function(req, res, next) { var hash = req.header("TrialPay-HMAC-MD5"), hmac = crypto.createHmac("md5", key); console.log(req.bodyRaw); hmac.update(req.bodyRaw) var crypted = hmac.digest("hex"); if(crypted === hash) { // Valid request User.findOne({ _id: req.body.user_id }, function (err, data) { if (err || !data) return res.error(500, "Internal server error"); data.deposit(req.body.amount / 100, false, "TrialPay Reward: "+req.body.oid, "offers", true); return res.send("1"); }); } else { // Invalid request return res.send("Invalid TrialPay hash", { "Content-Type": "text/plain" }, 403); } } } router.post("/", calculateSignature("[YOUR_KEY_HERE]")); module.exports = router; 

Thanks for the help and tips @AlexeyTen