Search code examples
dartdart-editordart-pub

Set the dart-editor server responses


My apache server is setup to respond to any request with the index.html. The idea behind this is to let the web app we are creating handle the routing. The dart-editor test environment does not do this by default.

Instead when i go to http://127.0.0.1.8080/something/that/does/not/exisit it will return a 404. I would like it to respond with the index.html and let the app handle the routing.

Is it possible to setup this behaviour for the dart test environment?


Solution

  • The suggested way is to use a custom server that acts as a proxy that forwards to pub serve.

    see also

    The code (copied from the linked issue)

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

    I use the route_hierarchical package in a way that it works the same with usePushState enabled or disabled. This way I can use URL fragments for development and pushState for deployment.
    See https://stackoverflow.com/a/25256858/217408 or the two similar (simple) examples where one uses usePushState false and the other true https://github.com/bwu-dart/bwu_polymer_routing