$http请求在node js上-如何成功返回值$http.post

$http request on node js - how to return a value in success $http.post

本文关键字:http 成功 post 返回值 何成功 请求 node js      更新时间:2023-09-26

我正在编写angular + node js应用程序,并且我有一个登录方法,使用http post调用验证用户和密码:

login.js

$scope.userLogin = function (info) {
            $http.post('/authenticate_user', {'name':info.name,'password':info.password})
            .success(function(data) {
            })
            .error(function(data) {
                console.log('Error:'+ data);
            });

        }
我正在处理身份验证的server.js文件中的

:

app.post('/authenticate_user', function(req, res){
    authenticate_user(req, res);
});

var authenticate_user=function(req, res){
 var query= user_details.find({'name': req.body.name});
 res.setHeader("Cache-Control", "private, no-cache, no-store, must-revalidate, max-age=0");
 query.exec( function(err, docs){
    if (docs.length==0) return false;
    res.json(docs[0].password==req.body.password);
});  
}

我想有userLogin函数在我的login.js文件有app.post()方法的值..

之类的
   $http.post('/authenticate_user', {'name':info.name,'password':info.password})
            .success(function(data) {
              // what is data includes here? the response for the http post?
              // or what the function I calling to aka authenticate_user, returns - in this case - a boolean value?
            })

我想从我的api后的值被转移到在login.js文件的调用方法..

你知道怎么做吗?

谢谢

返回一个承诺而不是隐藏它

$scope.userLogin = function (info) {
    return $http.post('/authenticate_user', info);
}
使用

userLogin({name: '', password: ''}).then(function(success) {
  console.log(success)
})