我正在尝试使用货架静态服务器来提供聚合物应用程序。我创建了下一个结构:
polymerapp - pubspec.yml - bin - server.dart - web - index.html - lib - 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时,它会向我显示下一个错误:
Failed to load resource: the server responded with a status of 404 (Not Found) http://localhost:9999/packages/paper_elements/roboto.html Failed to load resource: the server responded with a status of 404 (Not Found) http://localhost:9999/packages/polymertest/main_app.html Failed to load resource: the server responded with a status of 404 (Not Found) http://localhost:9999/packages/polymer/init.dart An error occurred loading file: package:polymer/init.dart
我想这样做是为了更快的开发方式。在这种情况下,每次我做出改变时,我都不需要构建聚合物飞镖应用程序。
答案 0 :(得分:2)
您可以将serveFilesOutsidePath: true
传递给createStaticHandler()
var handler = createStaticHandler(pathToBuild,
defaultDocument: 'index.html',
serveFilesOutsidePath: true);
此外,在开发期间,您可以将pub serve
与shelf_proxy
一起用于增量构建。有关示例,请参阅here。
答案 1 :(得分:0)
prod中的shelf shelf_static中的shelf_proxy组合非常有用。聪明的飞镖队提出了将这些结合起来的想法,我在莫吉托借用了这个想法。您可以按如下方式使用它
import 'package:mojito/mojito.dart';
final app = mojito.init();
app.router..addStaticAssetHandler('/ui');
代码为here,如果您愿意,可以复制