Perform Http get method in Angular with no retries and wait until server sends the response?

后端 未结 2 1898
猫巷女王i
猫巷女王i 2020-12-20 07:55

I have the following in my controller, in a Spring-Boot application:

@RequestMapping(method=RequestMethod.GET,value=\"/info\")
public DataModel getinfodata()         


        
相关标签:
2条回答
  • 2020-12-20 08:42

    The http observable makes an http request for each subscriber. So the 3 to 4 http request means you must have multiple components subscribing at the same time. To share a single http request for multiple observers, you need something like the share operator.

    export class FetchService {
        data$: Observable<any>;
    
        constructor(){
            this.data$ = this._http.request(this._url)
                .map((res:Response)=>res.json())
                .catch(this.errorHandler)
                .share();
        }
    
        getData(){
            return this.data$;
        }
    }
    

    Now the multiple observers will share the same observable.

    This operator is a specialization of publish which creates a subscription when the number of observers goes from zero to one, then shares that subscription with all subsequent observers until the number of observers returns to zero, at which point the subscription is disposed.


    As for your ERR_EMPTY_RESPONSE issue. I've run into this when my proxied api call timesout.

    0 讨论(0)
  • 2020-12-20 08:44

    Eventually, I figured out why it is happening The dev-server makes use http-proxy-middleware package More Here and the proxy options provided in this package is from underlying http-proxy library http-proxy options. one among them is

    proxyTimeout:

    timeout (in millis) when the proxy receives no response

    The default value is ~120 seconds(based on my observations) if the server fails to respond within the stipulated time(120s) a new request is made to the server. overriding the default timeout with "timeout":30000 in proxy config file resolved the issue.

    {
      "/info": {
         "target":  {
           "host": "localhost",
           "protocol": "http:",
           "port": 8080
         },
         "secure": false,
         "changeOrigin": true,
         "logLevel": "debug",
         "timeout":30000
      
      }
    }
    
    0 讨论(0)
提交回复
热议问题