简体   繁体   English

Angular + Nodejs API-处理请求响应的最佳实践

[英]Angular + Nodejs API - Best practice to handle request response

I'm writing my first API in node.js and front end in Angular. 我正在用node.js和Angular编写我的第一个API。

Wanted to ask what is the best practice for handling responses. 想问一下处理响应的最佳实践是什么。 So by this i mean should my API always return codes eg 403 or is a success = true / false fine? 因此,我的意思是我的API应该始终返回代码,例如403还是success = true / false

Is there any security consideration between the two, ie is one more secure? 两者之间是否有任何安全考虑,即更安全?

So in essence which of the 2 below should i use? 因此,从本质上讲,我应该使用以下哪两项?

if (res.data.success){
   //to do something
} 

vs

if (res.status == 403) {
   //to do something
}

Or am i worrying about nothing? 还是我什么都不担心?

Thanks. 谢谢。

The Good Practice is :- Below code itself handle errors It self dont Use if and Else .. use the Following type angular Update Best Praactice 优良作法是:-下面的代码本身处理错误,它自己不要使用if和Else ..使用以下类型的angular更新最佳实践

 $http({
      method: 'GET',
      url: '/someUrl'
    }).then(function successCallback(response) {
        // this callback will be called asynchronously
        // when the response is available
      }, function errorCallback(response) {
        // called asynchronously if an error occurs
        // or server returns response with an error status.
      });

Error responses automatically handled by pre-suported block errorcallback 通过预支持的块错误回调自动处理错误响应

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM