从Angular发布时在Express和req.body中发布的req以及如何协调?

时间:2015-05-27 22:03:29

标签: javascript node.js express

我有以下代码:

Module1.js

exports.create = function(req, res) {
     var answer = req.body.key;//this is already declared and works 
//just fine when sent from Angular but this is undefined when sent from
//express


     function something(){console.log(answer)}
}

单词数:

var confusing = require('../Module1Directory/Module1.js');

exports.create = function(req, res){
      var req = {key: value}
      confusing.create(req, res).something();//this is undefined
}

当我使用Angular中的$ http发布时req.body = {key:value} 当我从Express req = {key:value}和req.body = undefined 发布时 我如何处理这种差异或我错过了什么?

要清楚,我写了很多用req.body格式声明变量的代码,现在将变量更改为req只会非常棘手和耗时,就像在模块之间移植代码一样(并且有各种各样的凌乱的冗余)。

1 个答案:

答案 0 :(得分:1)

您在第二个模块中重新定义了req。

var req = {key: value};

使用它代替(显然这是一种不好的方法;如果你想要授予可维护性,你就不会覆盖请求体):

var req = { body: { key: value } };