如何使用Dart从其他网址提供静态文件? [英] How do I serve static files from a different URL with Dart?

查看:91
本文介绍了如何使用Dart从其他网址提供静态文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有了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 ^(。然后我所有的相对URL引用(到css / js / images) )break,如果我把它们从assets / whatever改写为/ assets / whatever,它会在Dart编辑器中工作时使用,因为它使用了以下URL:

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

想法?最佳做法?谢谢!


b 取决于您在Dart服务器VM之前是否有代理或Web服务器。如果您之前有代理,则代理可以在请求点击您的Dart VM之前重写网址。这是一个很好的方案,因为代理可以做缓存,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();
}

字符串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 );

final文件file = new File('$ {basePath} $ {path}');
file.exists()。then((found){
if(found){
file.fullPath()。then((string fullPath){
if .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)=>打印(错误);
server.listen('127.0.0.1',1337);
print('listen for connections on $ port');
}

main(){
var script = new File(new Options()。script);
var directory = script.directorySync();
runServer($ {directory.path},1337);
}


With Dart, I've got awesome.html, but I'd like it to be /awesome. Is this purely an .htaccess (I'm using Apache) thing, or is there a way to go about this the Dart or "modern web development" way?

This .htaccess bit directs /awesome to /awesome.html:

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

But then all my relative URL references (to css/js/images) break, and if I rewrite them from "assets/whatever" to "/assets/whatever" it'll break when working in the Dart Editor since it uses URLs like:

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

Ideas? Best practices? Thank you!

解决方案

thanks for the question!

The answer depends on if you have a proxy or web server in front of your Dart server VM. If you have a proxy in front, then the proxy can do the URL rewriting before the request hits your Dart VM. This is a nice scenario anyway, because a proxy can do caching, SSL, load balancing, and more. The Dart VM is then just an "app server" in this scenario. I would recommend placing an industrial strength web server or proxy in front just as a best practice.

However, if you want to do URL masking and rewriting purely in Dart, here is some code. As Kai says in the comments above, this is generally a framework's job. But I'll include some code here anyway for fun. :)

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

这篇关于如何使用Dart从其他网址提供静态文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆