8

ダートで、私は持っていますがawesome.html、私はそれが欲しい/awesomeです。これは純粋に.htaccess(私はApacheを使用しています)ものですか、それともこれをDartまたは「最新のWeb開発」の方法で実行する方法はありますか?

この.htaccessビットは次/awesomeの方向に向けられ/awesome.htmlます:

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .*[^/]$ %{REQUEST_URI}/ [L,R=301]
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^(.+)/$ $1.html [L]

しかし、その後、(css / js / imagesへの)すべての相対URL参照が壊れ、「assets/whatever」から「/assets/ whatever」に書き直すと、DartEditorで作業するときに次のようなURLを使用するため壊れます。 :

http://127.0.0.1:3030/Users/dave/Sites/my-dart-app/web/awesome.html

アイデア?ベストプラクティス?ありがとうございました!

4

2 に答える 2

5

質問ありがとう!

答えは、Dart サーバー VM の前にプロキシまたは Web サーバーがあるかどうかによって異なります。フロントにプロキシがある場合、リクエストが Dart VM に到達する前に、プロキシが URL の書き換えを実行できます。プロキシはキャッシング、SSL、負荷分散などを実行できるため、これはいずれにせよ良いシナリオです。Dart VM は、このシナリオでは単なる「アプリ サーバー」です。ベスト プラクティスとして、産業用強度の Web サーバーまたはプロキシを前面に配置することをお勧めします。

ただし、Dart だけで URL のマスキングと書き換えを行いたい場合は、次のコードを参照してください。上記のコメントで Kai が述べているように、これは通常、フレームワークの仕事です。しかし、楽しみのためにここにいくつかのコードを含めます。:)

import 'dart:io';
import 'dart:json';

class StaticFileHandler {
  final String basePath;

  StaticFileHandler(this.basePath);

  _send404(HttpResponse response) {
    response.statusCode = HttpStatus.NOT_FOUND;
    response.outputStream.close();
  }

  String rewritePath(String path) {
    String newPath = path;

    if (path == '/' || path.endsWith('/')) {
      newPath = '${path}index.html'; 
    } else if (!path.endsWith('.html')) {
      newPath = "${path}.html";
    }

    return newPath;
  }

  // TODO: etags, last-modified-since support
  onRequest(HttpRequest request, HttpResponse response) {
    String path = rewritePath(request.path);

    final File file = new File('${basePath}${path}');
    file.exists().then((found) {
      if (found) {
        file.fullPath().then((String fullPath) {
          if (!fullPath.startsWith(basePath)) {
            _send404(response);
          } else {
            file.openInputStream().pipe(response.outputStream);
          }
        });
      } else {
        _send404(response);
      }
    });
  }

}

runServer(String basePath, int port) {
  HttpServer server = new HttpServer();

  server.defaultRequestHandler = new StaticFileHandler(basePath).onRequest;
  server.onError = (error) => print(error);
  server.listen('127.0.0.1', 1337);
  print('listening for connections on $port');
}

main() {
  var script = new File(new Options().script);
  var directory = script.directorySync();
  runServer("${directory.path}", 1337);
}
于 2012-10-26T16:01:12.327 に答える
1

ちなみに、SethのコードのrewritePath()関数を更新して、.dartファイルや.cssファイルなどのアセットを.htmlに書き換えないようにし、クライアント側のものが存在する場合に機能するようにしました。 /ウェブ。

  String rewritePath(String path) {
    String newPath = path;

    if (path == '/' || path.endsWith('/')) {
      newPath = '/web${path}index.html';
    } else if (!path.endsWith('.html')) {
      if (path.contains('.')) {
        newPath = '/web${path}';
      } else {
        newPath = '/web${path}.html';
      }
    } else {
      newPath = '/web${path}.html';
    }

    //peek into how it's rewriting the paths
    print('$path -> $newPath');

    return newPath;
  }

もちろんこれは非常に基本的なことであり、ルーティングを処理するフレームワークは確かに便利です(@Kaiで何を構築しているのかを確認したいと思います)。

于 2012-11-07T02:06:38.270 に答える