HttpServerRequest:请求被处理了几次

HttpServerRequest: request is handled several times

我写了下面的代码:

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");
    }

奇怪的是,一旦我收到路径为“/healthcheck”的 GET 请求,我就会同时进入控制台:

OK

This request cannot be handled

我希望只得到 "OK",然后该方法必须 return。 你知道怎么做吗?

您可能收到多个请求,其中一个不是获取请求。能否通过插入日志语句来监控服务器

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

根据MDN

Preflighted requests unlike simple requests (discussed above), "preflighted" requests first send an HTTP OPTIONS request header to the resource on the other domain, in order to determine whether the actual request is safe to send. Cross-site requests are preflighted like this since they may have implications to user data

试试这个

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");
    }

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

GET localhost:8080/favicon 不满足条件,代码打印"This request cannot be handled"。