Search code examples
node.jsangularcorsangular-http-interceptors

Difference between Angular interceptors and CORS


I am reading about Angular Interceptors, and I don’t get one thing. Is there a need to use HTTP interceptors in Angular, if let’s say I use CORS in a backend in NodeJS to connect back and front. Do we use interceptors in case where i don’t have custom backend, or is it optional.

I was reading, but as I don’t a single app completely finished, and I am trying to understand the difference between those two. I couldn’t find an adequate answer on this topic.


Solution

  • What is an Angular HTTP Interceptor

    The angular interceptor is a medium connecting the backend and front-end applications. Whenever a request is made, the interceptors handle it in between. They can also identify the response by performing Rxjs operators. The interceptors do not initiate the handle method and handle the requests at their level.

    enter image description here

    Example when something you want to apply to all requests like in my case, used Interceptor to add 'Auth Token` to every request.

    More Detail Click Here

    Cross-Origin Resource Sharing (CORS)

    Cross-Origin Resource Sharing (CORS) is an HTTP-header based mechanism that allows a server to indicate any origins (domain, scheme, or port) other than its own from which a browser should permit loading resources. CORS also relies on a mechanism by which browsers make a "preflight" request to the server hosting the cross-origin resource, in order to check that the server will permit the actual request. In that preflight, the browser sends headers that indicate the HTTP method and headers that will be used in the actual request.

    enter image description here

    Example when you access one application from different domain, then you need to allow to access through CORS like in my case, my angular application in on localhost:4200 and web api on localhost:4236 so it throws CORS error until I allow my angular domain in my web api by setting CORS configuration in startup of web api.

    More Detail Click Here

    Don't confused, There is no link between them.