【问题标题】:How to serve a polymer application from dart shelf static file handler?如何从飞镖架静态文件处理程序提供聚合物应用程序?
【发布时间】:2015-04-12 11:59:07
【问题描述】:

我正在尝试使用架子静态服务器为聚合物应用程序提供服务。我创建下一个结构:

聚合物应用 - pubspec.yml - 斌 -server.dart - 网络 - index.html - 库 - main_app.dart - main_app.html

在 server.dart 里面我放了这段代码:

import 'dart:io' show Platform;
import 'dart:async' show runZoned;
import 'package:path/path.dart' show join, dirname;
import 'package:shelf/shelf_io.dart' as io;
import 'package:shelf_static/shelf_static.dart';

void main() {
  // Assumes the server lives in bin/ and that `pub build` ran
  var pathToBuild = join(dirname(Platform.script.toFilePath()),
      '..', 'web');

  var handler = createStaticHandler(pathToBuild,
      defaultDocument: 'index.html');

  var portEnv = Platform.environment['PORT'];
  var port = portEnv == null ? 9999 : int.parse(portEnv);

  runZoned(() {
    io.serve(handler, '0.0.0.0', port);
    print("Serving $pathToBuild on port $port");
  },
  onError: (e, stackTrace) => print('Oh noes! $e $stackTrace'));
}

剩下的就是 dart 编辑器创建的模板聚合物应用程序。

问题是当我尝试从浏览器访问 localhost:9999 时,它会显示下一个错误:

加载资源失败:服务器响应状态为 404(未找到) http://localhost:9999/packages/paper_elements/roboto.html 加载资源失败:服务器响应状态为 404(未找到) http://localhost:9999/packages/polymertest/main_app.html 加载资源失败:服务器响应状态为 404(未找到) http://localhost:9999/packages/polymer/init.dart 加载文件时出错:package:polymer/init.dart

我想这样做是为了更快的开发方式。在这种情况下,我不需要在每次进行更改时都构建聚合物飞镖应用程序。

【问题讨论】:

    标签: dart dart-polymer dart-shelf


    【解决方案1】:

    dev 中的shelf_proxy 和prod 中的shelf_static 的组合非常有用。聪明的飞镖团队想出了将这些结合起来的想法,我在 mojito 中借用了这个想法。你可以如下使用它

    import 'package:mojito/mojito.dart';
    
    final app = mojito.init();
    
    app.router..addStaticAssetHandler('/ui');
    

    代码是here,如果您愿意,可以复制它

    【讨论】:

      【解决方案2】:

      您可以将serveFilesOutsidePath: true 传递给createStaticHandler()

       var handler = createStaticHandler(pathToBuild,
         defaultDocument: 'index.html',
         serveFilesOutsidePath: true);
      

      此外,在开发过程中,您可以使用 pub serveshelf_proxy 进行增量构建。示例见here

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-10-20
        • 2017-04-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-08-24
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多