2017-05-10 33 views
1

我写了下面的代码:HttpServerRequest:请求被处理几次

public static void handleRequest(HttpServerRequest request, Vertx vertx) throws FileNotFoundException { 
     if (request.method() == HttpMethod.GET) { 
      if (request.path().equals("/healthcheck")) { 
       returnResponse(request, "I'm alive!!!\n", true); 
       System.out.println("OK"); 
       return; 
      } 
      ... 

     } 
     returnResponse(request, "Not Valid Request", false); 
     System.out.println("This request cannot be handled"); 
    } 

怪异的是,一旦我得到与路径“/健康检查”的GET请求,我在控制台都得到:

OK

这个请求无法处理

我希望只能得到“OK”,然后该方法必须返回。 你知道如何做到这一点吗?

回答

2

您可能获得多个请求,其中一个请求不是获取请求。您可以通过插入日志语句来监视服务器:

0

您正在尝试处理预检查请求。

根据MDN

不同于简单的请求(上面讨论的), “预检”请求第一发送一个HTTP OPTIONS请求头 对其他域的资源,以确定是否

预检请求 实际请求可以安全发送。跨站点请求预检 这样,因为他们可能影响到用户数据

试试这个,而不是

public static void handleRequest(HttpServerRequest request, Vertx vertx) throws FileNotFoundException { 
     if(request.method() == HttpMethod.OPTIONS) { 
      return; 
     } 

     if (request.method() == HttpMethod.GET) { 
      if (request.path().equals("/healthcheck")) { 
       returnResponse(request, "I'm alive!!!\n", true); 
       System.out.println("OK"); 
       return; 
      } 
      ... 

     } 
     returnResponse(request, "Not Valid Request", false); 
     System.out.println("This request cannot be handled"); 
    } 
1

我终于发现,我的浏览器发送两个请求。 第一个请求是GET localhost:8080/healthcheck,第二个请求是GET localhost:8080/favicon。

GET localhost:8080/favicon不符合条件,代码显示“此请求无法处理”。