1

您好我正在尝试创建一个 Web 应用程序,您可以在其中通过 Web 浏览器访问系统上的文件。Web 应用程序结构如下所示:

指挥官

  cmdr
       packages
       lib
       cmdr.dart

  gui
      packages
      web
            assets
            css
            console.dart
            editor.dart
            client.dart
            explorer.dart
            index.html

cmdr.dart是服务器,我在那里启动http_server主机index.html。索引将client.dart文件作为脚本拉取。editor.dart和文件都是客户端文件的 console.dart一部分。explorer.dart

问题是当我运行http_serverto hostindex.html时,它不会访问客户端文件。除了 CSS 文件之外,大多数依赖项都不会被提取。此外,我认为也许编译成 javascript 可以解决这个问题,因为它将所有代码合并到一个文件中。然而,同样的问题存在于将 HTML 放在一起但没有创建任何客户端组件的情况下。

我的服务器代码如下:

// Setting up Virtual Directory

VirtualDirectory virDir;

void directoryHandler(dir, request) {
  var indexUri = new Uri.file(dir.path).resolve('index.html');
  virDir.serveFile(new File(indexUri.toFilePath()), request);
}

void main(List<String> args) {
  // Set default dir as current working directory.
  Directory dir = Directory.current;

  // Creating Virtual Directory
  virDir = new VirtualDirectory(Platform.script.resolve('/Users/donghuynh/git/commander/gui/web').toFilePath())
      ..allowDirectoryListing = true
      ..directoryHandler = directoryHandler
      ..followLinks = true;

  // Set up logging.
  log = new Logger('server');
  Logger.root.onRecord.listen(new SyncFileLoggingHandler("server.log"));

  // Create an args parser to override the workspace directory if one is supplied.
  var parser = new ArgParser();
  parser.addOption('directory', abbr: 'd', defaultsTo: Directory.current.toString(), callback: (directory) {
    dir = new Directory(directory);
  });
  parser.parse(args);

  // Initialize the DirectoryWatcher.
  watcher = new DirectoryWatcher(dir.path);

  // Set up an HTTP webserver and listen for standard page requests or upgraded
  // [WebSocket] requests.
  HttpServer.bind(InternetAddress.ANY_IP_V4, 8080).then((HttpServer server) {
    log.info("HttpServer listening on port:${server.port}...");
    server.listen((HttpRequest request) {
      // WebSocket requests are considered "upgraded" HTTP requests.
      if (WebSocketTransformer.isUpgradeRequest(request)) {
        log.info("Upgraded ${request.method} request for: ${request.uri.path}");
        WebSocketTransformer.upgrade(request).then((WebSocket ws) {
          handleWebSocket(ws, dir);
        });
      } else {
        log.info("Regular ${request.method} request for: ${request.uri.path}");
        // TODO: serve regular HTTP requests such as GET pages, etc.
        virDir.serveRequest(request);
      }
    });
  });
}

通过网络浏览器访问 index.html 我收到以下错误:

http://localhost:8080/packages/bootjack/css/bootstrap.min.css Failed to load resource: the server responded with a status of 404 (Not Found)
http://localhost:8080/packages/ace/src/js/ext-language_tools.js Failed to load resource: the server responded with a status of 404 (Not Found)
http://localhost:8080/packages/ace/src/js/ace.js Failed to load resource: the server responded with a status of 404 (Not Found)
http://localhost:8080/packages/browser/dart.js Failed to load resource: the server responded with a status of 404 (Not Found)
http://localhost:8080/packages/bootjack/css/bootstrap.min.css Failed to load resource: the server responded with a status of 404 (Not Found)

这些文件实际上存在于这些位置(尽管符号链接),但由于某种原因它们没有被拉出。

-大学教师

4

1 回答 1

0

据我记得,您需要传递一个额外的参数VirtualDirectory来遵循符号链接。

无论如何,您都不应该直接提供 Dart 源,而是提供来自pub build(至少用于生产)的输出。对于开发,将请求转发到正在运行的pub serve实例可能是更好的方式,也是官方建议的方式。

于 2014-12-17T06:39:09.967 回答