Dart http_server serving index.html but not pulling dependencies

508 views Asked by At

Hi I'm trying to create a web app where you can access files on a system through a web browser. The web app structure looks like this:

commander

  cmdr
       packages
       lib
       cmdr.dart

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

cmdr.dart is the server and I initiate the http_server to host index.html on there. The index pulls the client.dart file as a script. The editor.dart, console.dart and explorer.dart files are all part of the client file.

The problem is when I run the http_server to host index.html, it doesn't access the client files. Most of the dependencies aren't pulled except for the CSS files. Furthermore I thought maybe compiling into javascript would solve this issue as it would pull all the code together into one file. However the same issue exists where the HTML gets put together but none of the client components are created.

My server code is as follows:

// 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);
      }
    });
  });
}

Acessing index.html through a web browser I get these errors:

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)

The files actually exist in those locations (though sym linked) but they are not being pulled for some reason.

-Don

1

There are 1 answers

0
Günter Zöchbauer On

As far as I remember you need to pass an additional argument to VirtualDirectory to follow symlinks.

You shoud'nt serve Dart source directly anyway but instead the output from pub build (at least for production). For development, forwarding requests to a running pub serve instance is probably the better and also the officially suggested way.