gpt4 book ai didi

Dart http_server 服务 index.html 但不拉依赖

转载 作者:行者123 更新时间:2023-12-03 04:06:08 31 4
gpt4 key购买 nike

您好我正在尝试创建一个 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.dartexplorer.dart文件都是客户端文件的一部分。

问题是当我运行 http_server主持 index.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)

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

-大学教师

最佳答案

据我记得,您需要向 VirtualDirectory 传递一个附加参数。遵循符号链接(symbolic link)。

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

关于Dart http_server 服务 index.html 但不拉依赖,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27517430/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com