Dart语言:GET(favicon问题)

时间:2014-06-26 21:55:14

标签: dart dart-io

我在服务器端运行了一个Dart应用程序。它正在一个特定的端口监听并正常工作。问题是:我的听众正在回应最喜欢的图标(favicon)的GET。

我该如何避免?

编辑:给出一些代码示例。

import 'dart:io';

void main() {
    print("Starting server.");
    HttpServer.bind(InternetAddress.LOOPBACK_IP_V4, 4041)
        .then(listenForRequests)
        .catchError((e) => print (e.toString()));
}

listenForRequests(HttpServer _server) {
    _server.listen((HttpRequest request) {
        if (request.method == 'GET') {
            handleGet(request);
        } else {
            request.response.statusCode = HttpStatus.METHOD_NOT_ALLOWED;
            request.response.write("Unsupported request: ${request.method}.");
            request.response.close();
        }
    },
    onDone: () => print('No more requests.'),
    onError: (e) => print(e.toString()) );
}

void handleGet(HttpRequest request) {
    int requestNumber = 1;
    print(requestNumber); // This shows me the request number. Just for information.
    print(request.uri); // This shows me the request from the client browser.
    request.response.statusCode = HttpStatus.OK;
    request.response.close();
}

这是此代码的输出:

1

  

/ SOME_REQUEST_FROM_THE_BROWSER

2

  

/favicon.ico

1 个答案:

答案 0 :(得分:2)

您可以检查所请求的资源,并针对“&favicon.ico”的请求生成正确的响应。像

void handleGet(HttpRequest request) {
    int requestNumber = 1;
    print(requestNumber++); // This shows me the request number.
    print(request.uri); // This shows me the request from the client browser.
    if(request.requestedUri.path != '/favicon.ico') {
      request.response.statusCode = HttpStatus.NOT_FOUND;
    } else {
      request.response.statusCode = HttpStatus.OK;
    }
    request.response.close();
}