[英]Can someone explain how this http status code is processed?
我想知道~(event.status / 100) > 3
在從此處獲取的以下代碼中正在做什么?
~
這里? @Injectable()
export class AuthInterceptor implements HttpInterceptor {
intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
console.info('req.headers =', req.headers, ';');
return next.handle(req)
.map((event: HttpEvent<any>) => {
if (event instanceof HttpResponse && ~(event.status / 100) > 3) {
console.info('HttpResponse::event =', event, ';');
} else console.info('event =', event, ';');
return event;
})
.catch((err: any, caught) => {
if (err instanceof HttpErrorResponse) {
if (err.status === 403) {
console.info('err.error =', err.error, ';');
}
return Observable.throw(err);
}
});
}
}
~
是按位NOT運算符。 MDN
~(event.status / 100) > 3
與event.status <= -500
相同。
請參見下面的代碼段。
a = 200; console.log( ~(a / 100) , ~(a / 100) >3 ); a = 300; console.log( ~(a / 100), ~(a / 100) >3 ); a = 400; console.log( ~(a / 100), ~(a / 100) >3 ); a = -200; console.log( ~(a / 100), ~(a / 100) >3 ); a = -300; console.log( ~(a / 100), ~(a / 100) >3 ); a = -400; console.log( ~(a / 100), ~(a / 100) >3 ); a = -500; console.log( ~(a / 100), ~(a / 100) >3 );
該代碼確定HTTP響應是信息響應還是成功的響應。 由於信息和成功響應的范圍是響應代碼100到226。您可以在https://developer.mozilla.org/en-US/docs/Web/HTTP/Status上查看HTTP響應代碼列表。 HTTP響應代碼只是告訴您與URL的連接的當前狀態。 例如,如果代碼是226,則表示Web服務器處於“ IM已使用”狀態。
對於您顯示的代碼,它正在將信息和成功的代碼記錄到控制台,而其他代碼則被允許進入並進入需要該信息的代碼。
我希望這將幫助您了解代碼的功能。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.