8

使用 Dart,我有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]

但是然后我所有的相对 URL 引用(到 css/js/images)都中断了,如果我将它们从“assets/whatever”重写为“/assets/whatever”,它会在 Dart 编辑器中工作时中断,因为它使用 URL 之类的:

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

想法?最佳实践?谢谢!

4

2 回答 2

5

谢谢你的提问!

答案取决于您的 Dart 服务器虚拟机前面是否有代理或 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

By the way, I've updated the rewritePath() function in Seth's code some so that it doesn't rewrite assets like .dart and .css files to .html, and so that it works w/ my client-side stuff living in /web.

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

It's of course super basic, and a framework that handles routing would certainly come in handy (would love to see what you're building @Kai).

于 2012-11-07T02:06:38.270 回答