2

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?

Günter Zöchbauer
  • 623,577
  • 216
  • 2,003
  • 1,567
Funonly
  • 283
  • 1
  • 13

1 Answers1

2

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

Community
  • 1
  • 1
Günter Zöchbauer
  • 623,577
  • 216
  • 2,003
  • 1,567
  • The import 'dart:html' is not available on the stand-alone VM. So i cannot run this in the dart-editor – Funonly Aug 12 '14 at 11:18
  • This should become a console application that is run individually from your application you are currently developing. The imports are probably `dart:io` and maybe `dart.http`. You launch your entry page using DartEditor with a custom launch configuration that points to this proxy. (I haven't used this myself yet) – Günter Zöchbauer Aug 12 '14 at 11:20