How do I configure an HTTP server to serve a client with package dependencies?
I have a project with a server and a client (each in its own directory and setting up separate 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 am unable to get the 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 VirtualDirectory directly to the client / web, then normal static files work, but then nothing outside (namely the package directory) is pulled in. And if I try to serve only the client the directory and specify web / index.html in the handler then nothing else gets. I also tried copying the package directory to the net, but they don't seem to make it either.
Note, I've seen other examples where HttpServer points to the build directory, but I'm trying to test my project before I compile the dart to js.
Overall, I am just confused about how to set up a server / client project where the server actually serves the client and the client has its own packages. I cannot find any literature on the internet for this particular case.
source to share
Maybe setting jailRoot
of virDir
to false is what you want, but what is the directory maintenance point client/web
? When you run pub build
for your client package, you end up with inline code that contains no symbolic links, minifies and trembles from the tree. Dart source code should not be serviced by clients. Even if you are using Dart-enabled browsers, you must run pub build
with dart2dart
(not yet verified) in your source code. For development, the suggested practice is to send requests pub serve
to your custom server.
source to share