您好我正在尝试创建一个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)
文件实际存在于这些位置(虽然是sym链接的)但是由于某种原因它们没有被拉出。
-Don
答案 0 :(得分:0)
据我记得,您需要将另一个参数传递给VirtualDirectory
以遵循符号链接。
你不应该直接为Dart源提供服务,而是来自pub build
的输出(至少用于制作)。对于开发,将请求转发到正在运行的pub serve
实例可能是更好的,也是官方建议的方式。