【问题标题】:Set the dart-editor server responses设置 dart-editor 服务器响应
【发布时间】:2014-10-05 08:48:30
【问题描述】:

我的 apache 服务器设置为使用 index.html 响应任何请求。这背后的想法是让我们正在创建的 Web 应用程序处理路由。默认情况下,dart-editor 测试环境不这样做。

当我转到http://127.0.0.1.8080/something/that/does/not/exisit 时,它会返回 404。我希望它用 index.html 响应并让应用处理路由。

是否可以为 dart 测试环境设置此行为?

【问题讨论】:

    标签: dart dart-editor dart-pub


    【解决方案1】:

    建议的方法是使用自定义服务器作为转发到pub serve 的代理。

    另见

    代码(从链接问题复制)

    Future proxyToPub(HttpRequest request, String path) {
        const RESPONSE_HEADERS = const [
            HttpHeaders.CONTENT_LENGTH,
            HttpHeaders.CONTENT_TYPE ];
    
        var uri = pubServeUrl.resolve(path);
        return client.openUrl(request.method, uri)
            .then((proxyRequest) {
              proxyRequest.headers.removeAll(HttpHeaders.ACCEPT_ENCODING);
              return proxyRequest.close();
            })
            .then((proxyResponse) {
              proxyResponse.headers.forEach((name, values) {
                if (RESPONSE_HEADERS.contains(name)) {
                  request.response.headers.set(name, values);
                }
              });
              request.response.statusCode = proxyResponse.statusCode;
              request.response.reasonPhrase = proxyResponse.reasonPhrase;
              return proxyResponse.pipe(request.response);
            })
            .catchError((e) {
              print("Unable to connect to 'pub serve' for '${request.uri}': $e");
              var error = new AssetError(
                  "Unable to connect to 'pub serve' for '${request.uri}': $e");
              return new Future.error(error);
            });
      }
    

    我使用route_hierarchical 包的方式与启用或禁用usePushState 相同。这样我就可以使用 URL 片段进行开发,使用 pushState 进行部署。
    https://stackoverflow.com/a/25256858/217408 或两个类似的(简单)示例,其中一个使用 usePushState false 另一个使用 true https://github.com/bwu-dart/bwu_polymer_routing

    【讨论】:

    • 导入 'dart:html' 在独立 VM 上不可用。所以我不能在飞镖编辑器中运行它
    • 这应该成为一个控制台应用程序,可以从您当前正在开发的应用程序中单独运行。导入可能是dart:iodart.http。您使用 DartEditor 启动您的入口页面,并使用指向此代理的自定义启动配置。 (我自己还没用过)
    猜你喜欢
    • 2020-03-29
    • 2016-04-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-04
    • 2016-08-02
    • 2021-07-22
    相关资源
    最近更新 更多