如何在proxy.conf.json angular中使用拦截器

时间:2021-08-01 17:01:22

标签: angular express cors

我需要对请求使用拦截器。而且我还需要使用 proxy.conf.json 文件来避免 CORS 错误。如何同时使用拦截器和proxy.conf.json?

{
  "/api/*": {
    "target": "http://localhost:9000",
    "secure": false,
    "logLevel": "debug"
  }
}

拦截器

@Injectable()
export class EndpointInterceptor implements HttpInterceptor {

  private static addRequestId(request: HttpRequest<any>): HttpRequest<any> {
    return request.clone({
      withCredentials: true,
      headers: request.headers
    });
  }
  intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
    if (req.url.startsWith('./assets/')) {
      return next.handle(req);
    }

    req = EndpointInterceptor.addRequestId(req);

    console.log(environment.dev);

    return next.handle(req.clone({ url: `${ environment.dev }${ req.url }` }));
  }
}

0 个答案:

没有答案