Express.js:将req.body转换为POST编码的string

我正在使用express.bodyParser中间件,我试图将req.body对象转换为POST编码的string。 有没有办法做到这一点?

例:

Name: Jonathan Doe Age: 23 Formula: a + b == 13%! 

变为:

 Name=Jonathan+Doe&Age=23&Formula=a+%2B+b+%3D%3D+13%25%21 

节点有一个这个模块。

 var qs = require('querystring'); ... console.log(qs.stringify(req.body)); 

但无论如何连接/快递存储在req.rawBody的原料。

我认为这应该相当简单 – 你应该能够像在浏览器中那样做。 此函数将对象/数组的所有string/数字成员转换为可用作POST正文的string:

 var objectToPostBody = function (object) { var i, out; if (!object) { return false; } out = []; for (i in object) { if (typeof object[i] === 'string' || typeof object[i] === 'number') { out[out.length] = encodeURIComponent(i) + '=' + encodeURIComponent(object[i]); } } return out.join('&'); }; 

如果你想处理子数组/子对象,这个函数会变得更加复杂,但是对于你上面描述的内容,我认为这应该是个诀窍。