2012-10-26 44 views
7

隨着Dart,我有awesome.html,但我想它是/awesome。這是純粹的.htaccess(我使用Apache)的東西,還是有辦法去這個飛鏢或「現代網絡開發」的方式嗎?如何使用Dart從不同的URL提供靜態文件?

.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 /圖片)休息,如果我從它們改寫 「資產/不管」 到「/資產/無論什麼「在Dart編輯器中工作時會中斷,因爲它使用以下URL:

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

想法?最佳實踐?謝謝!

+2

我相信一個框架在這裏會有很大的幫助。就目前來看,沒有簡單的方法可以做到這一點,所以需要一個路由系統。實際上我正在研究一個,但還沒有準備好。 –

+0

我假設你在談論Dart服務器端應用程序? –

+0

@SethLadd,現在它只是一組簡單的客戶端頁面,儘管我明白我需要一個服務器端應用程序來讓我的頁面服務更時髦,如下面的答案。 –

回答

4

感謝您的問題!

答案取決於您的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); 
} 
+0

謝謝塞斯!我從聊天應用程序中識別出這些代碼。是的,我想像你說的那樣,我想用「前端的工業強度的Web服務器或代理」。你能幫我理解代理和Web服務器之間的區別嗎?你會推薦哪個代理服務器或Web服務器?曾經是一個長期的LAMP人,所以Apache就是我習慣的,但是有服務器端堆棧的推薦嗎? –

+2

我會說「隨你所知」,Apache可以作爲代理。 Apache或nginx可能更高性能的服務原始靜態文件,但這是一個未經測試的客戶。這會讓你的Dart虛擬機成爲你的「應用服務器」。 –

+2

爲了擴展Seth的最後一條評論,Apache和nginx在服務靜態文件夾(至少現在是這樣)中速度更快,不過要讓Apache緩存內存中較小的靜態文件並且Apache做一些愚蠢的事情(比如解析)很困難。每次請求時遞歸的htaccess'文件。我個人建議使用nginx或Dart來提供靜態文件,但這只是我的看法。 –

0

順便說一句,我已經更新了rewritePath()函數在塞特的一些代碼,以便它不重寫像.dart和.css文件資產.HTML,所以它的工作原理w /我的客戶端的東西生活在/網絡。

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)。