嗨,我正在尝试创建一个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_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
实例可能是更好的方法,也是正式建议的方法。
关于dart - Dart http_server提供index.html,但不提取依赖项,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/27517430/