2016-04-03 55 views
1

我是dart的編程新手,我正在構建dart應用程序,我想從服務器端啓動dart應用程序。喜歡加載我的主頁,當我輸入我的網頁的網址。從服務器啓動dart應用程序並加載元素

在我的服務器端我有這樣的代碼,我從教程了從DART的網頁:

var server; 

    try{ 
    server = await HttpServer.bind(InternetAddress.LOOPBACK_IP_V6, 4040); 
    }catch(e){ 
    print("Couldn't bind to port 4044: $e"); 
    exit(-1); 
    } 
    await for(HttpRequest req in server){ 
    var file = new File('index.html'); 

    if(await file.exists()){ 
     print("Serving index.html."); 
     req.response.headers.contentType = ContentType.HTML; 
     try{ 
     await file.openRead().pipe(req.response); 
     }catch(e){ 
     print("Couldn't read file: $e"); 
     exit(-1); 
     } 
    }else{ 
     print("Couldn't open index.html"); 
     req.response..statusCode = HttpStatus.NOT_FOUND..close(); 
    } 
    } 

但現在我的問題是關於客戶端,我的元素不加載,如CSS,圖像等。

You can see the appearance here

我想我需要在我的服務器端設置的東西加載。它是什麼?

+0

我假設你的服務你的飛鏢服務器所產生的運行上的飛鏢瀏覽器應用程序'酒館build'的輸出('編譯/網絡/ **')? –

回答

1

使用核心dart:io提供文件的方法往往不是最簡單的方法。看看shelf,f.e.這是一個非常整潔的框架,用於創建服務器應用程序,這些應用程序有很多中間件的插件和擴展等。

就您而言,您似乎只想提供靜態內容。我建議你使用shelf_static。它會是這樣的:

import 'package:shelf/shelf_io.dart' as io; 
import 'package:shelf_static/shelf_static.dart'; 

void main() { 
    var handler = createStaticHandler('your-web-directory/', 
     defaultDocument: 'index.html'); 

    io.serve(handler, InternetAddress.LOOPBACK_IP_V6, 4040); 
} 
相關問題