ExpressJS AngularJS POST

ExpressJS AngularJS POST,angularjs,node.js,express,Angularjs,Node.js,Express,我正在学习AngularJS,我想知道如何使用ExpressJS正确地将其与节点连接起来 这是我的控制器: app.controller('view1Ctrl', function($scope, $http) { $scope.sub = function(desc) { console.log(desc); $http.post('/view1', desc). then(function(response) {

我正在学习AngularJS,我想知道如何使用ExpressJS正确地将其与节点连接起来

这是我的控制器:

app.controller('view1Ctrl', function($scope, $http) {

    $scope.sub = function(desc) {
        console.log(desc);
        $http.post('/view1', desc).
        then(function(response) {
            console.log("posted successfully");
        }).catch(function(response) {
            console.error("error in posting");
        })
    }
});
这是我的server.js:

app.post('/view1', function(req, res) {
    console.log(res.desc);
    res.end();
});
我没有使用主体解析器。当我在控制器中使用函数时,我不知道如何使用主体解析器从html中获取表单值。使用主体解析器时,是否从单击submit时的html中获取值,或者是否从作为参数传递表单值的函数中获取值。请告诉我是怎么做的

编辑:这是我的html:

<form>
      <input type="text" ng-model="desc" placeholder="Enter desc" />
      <button class="btn btn-primary" ng-click="sub(desc)">Submit</button>
</form>
编辑3: 编辑控制器app.js

app.controller('view1Ctrl', function($scope, $http) {    
    $scope.sub = function() {
        console.log($scope.formData);
        $http.post('/view1', $scope.formData).
        success(function(data) {
            console.log("posted successfully");
        }).error(function(data) {
            console.error("error in posting");
        })
    };
});

Node.js(Express)的body parser模块可以将表单post中的每个数据获取到一个名为
req.body
的对象中,因此,如果您有一个
$scope
对象来定义表单数据,您可以直接插入该对象,以便在req.body上复制相同的属性:

角度:

app.controller('view1Ctrl', function($scope, $http) {
    $scope.sub = function() {
        $http.post('/view1',$scope.formData).
        then(function(response) {
            console.log("posted successfully");
        }).catch(function(response) {
            console.error("error in posting");
        })
    }
});
HTML:

您也可以在表单元素中使用
ng submit
来代替ng单击submit按钮:

<form ng-submit="sub()">

首先,您应该了解两个全局变量
req
res

当您点击post请求时,
req.body
http
捕获请求,
body解析器
从post请求中提取原始内容

app.post('/view1', function(req, res) {
 console.log(req.body.desc);
 res.end();
});
在使用它之前,你必须包括

var bodyParser = require('body-parser');
并将中间件作为

app.use(bodyParser.json()); // for parsing application/json
app.use(bodyParser.urlencoded({ extended: true })); // for parsing       application/x-www-form-urlencoded
有关
中间件
req
res
的更多信息,请参阅


所以在我的html中,我不需要传递desc变量?我应该调用sub()吗?当然,angular的双向数据绑定为您提供了这一功能。当您在视图中定义ng model='desc'时,您可以在控制器中使用$scope.desc.yeah,无需传递它,$scope对象包含您定义控制器的区域内的所有范围变量。并且req.body是否仅包含该特定表单内的值?也可以在答案中添加控制器。我还是不明白。
app.use(bodyParser.json())
是给我的!哦,老兄,那太令人沮丧了。那按钮的咔嗒声呢?它应该再次通过desc还是我只需要调用它而不传递任何内容?你不需要它,你有
$scope
,我正在更新答案。在所有修改之后,它显示TypeError:无法读取未定义的属性'desc'。我实际上在节点服务器中得到了该错误。!它说desc未定义。执行
console.log(req.body)
app.post('/view1', function(req, res) {
 console.log(req.body.desc);
 res.end();
});
var bodyParser = require('body-parser');
app.use(bodyParser.json()); // for parsing application/json
app.use(bodyParser.urlencoded({ extended: true })); // for parsing       application/x-www-form-urlencoded