Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set up HTTP server to serve a client with package dependencies of its own?

I've a got a project with a server and client (each in their own directory and set up individual packages), so:

$ ls -l project/server
pubspec.yaml
server.dart
packages

$ ls -l project/client
pubspec.yaml
packages
web

$ ls -l project/client/web
index.html
main.css
client.dart

Now I'm having trouble getting HttpServer to serve anything outside of the web directory with the following code:

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

void main() {
  Logger.root.onRecord.listen(new SyncFileLoggingHandler("server.log"));

  virDir = new VirtualDirectory(Platform.script.resolve('../client/web').toFilePath())
    ..allowDirectoryListing = true
    ..followLinks = true
    ..directoryHandler = directoryHandler;

  HttpServer.bind(InternetAddress.ANY_IP_V4, 8080).then((HttpServer server) {
    log.info("HttpServer listening on port:${server.port}...");
    server.listen((HttpRequest request) {
      if (WebSocketTransformer.isUpgradeRequest(request)) {
        log.info("Upgraded ${request.method} request for: ${request.uri.path}");
        WebSocketTransformer.upgrade(request).then(handleWebSocket);
      } else {
        log.info("Regular ${request.method} request for: ${request.uri.path}");
        virDir.serveRequest(request);
      }
    });
  });
}

And in index.html:

  <script src="../packages/browser/dart.js"></script>

It seems that if I resolve the VirtualDirectory directly to client/web then the usual static files work, but then anything outside (namely, the packages directory) doesn't get pulled in. And if I try to serve just the client directory and specify web/index.html in the handler, then nothing else gets picked up. I've also tried copying the packages directory into web, but they don't seem to get picked up that way either.

Note, I have seen other examples where HttpServer points to the build directory, but I'm trying to test my project before compiling the dart to js.

Overall, I'm just confused about how to set up a server/client project where the server actually serves the client and the client has packages of its own. I can't seem to find any literature online for this specific case.

like image 563
Crunchex Avatar asked Jan 24 '26 07:01

Crunchex


1 Answers

Maybe setting jailRoot of virDir to false is what you want but what's the point of serving the client/web directory? When you run pub build for your client package you get built code that doesn't contain any symlinks, is minified and tree-shaken. The Dart source code is not supposed to be served to clients. Even when you serve to Dart-capable browsers you are supposed to run pub build with dart2dart (not well suppored yet) on your source code. For development the suggested practice is to forward requests to pub serve in your custom server.

like image 107
Günter Zöchbauer Avatar answered Jan 25 '26 23:01

Günter Zöchbauer



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!