Angular + Nodejs API - 处理请求响应的最佳实践
问题描述:
我在node.js和Angular的前端编写我的第一个API。Angular + Nodejs API - 处理请求响应的最佳实践
想要问什么是处理回复的最佳做法。所以,我的意思是我的API总是返回代码,例如403
或者是success
= true
/false
罚款?
这两者之间是否存在任何安全考虑因素,即是否更安全?
因此,本质上哪两个我应该使用?
if (res.data.success){
//to do something
}
VS
if (res.status == 403) {
//to do something
}
还是我担心什么?
谢谢。
答
的良好做法是: - 下面的代码本身的错误处理它自身不使用if和else ..使用以下类型的角度更新最佳Praactice
$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.
});
错误响应由预自动处理 - 导出的块errorcallback
好的 - 我看到这是有道理的。 如果我选择在if语句中选择我的方式,是否存在任何错误?在安全漏洞方面错误? – userMod2
你知道我试图做,如果如果和其他..问题是错误不被捕获,如果其他块idk的实际原因,但.. http错误处理是比其他方式更好肯定.. – Prasad
酷 - 好Angular文档建议这样做让我们坚持:-) 感谢您的帮助队友 – userMod2