【发布时间】:2014-06-05 17:00:10
【问题描述】:
我有一个简单的 Dart HTTPServer 正在运行,它为virDir.serveRequest(request); 提供请求,它适用于 URL 192.168.1.200:8080/index.html,但如果我使用 192.168.1.200:8080 或 192.168.1.200:8080/ 则提供 404 Not Found。我可能天真,尽管默认是自动的。顺便说一句,这对我来说是全新的。
我没有注意到 HTMLServer 中的任何默认设置,这是如何实现的?
(编辑)
我已经能够检测到使用默认值并计算出正确的文件名,但不明白如何将其传递给浏览器:
_processRequest(newPath, request) { // new path is index.html
File file = new File(newPath);
file.exists().then((found) {
if(found) {
file.openRead().pipe(request.response); // probably dies here
}
else {
_send404(request.response);
}
});
}
[编辑]
仍然无法提供 index.html 文件。我尝试使用 VirtualDirector.serveFile() 但在尝试处理默认 index.html 文件时无法使其工作。我一直在尝试效仿。
final HTTP_ROOT_PATH = Platform.script.resolve('../web').toFilePath();
final virDir = new VirtualDirectory(HTTP_ROOT_PATH)
..jailRoot = false // process links will work
..followLinks = true
..allowDirectoryListing = true;
var dir = new Directory(virDir.root);
var indexUri = new Uri.file(dir.path).resolve('/index.html');
virDir.serveFile(new File(indexUri.toFilePath()), request);
当我运行它并打印 indexUri.toFilePath() 时,输出是 '/index.html'
我的代码位于 /srv/fireimager/bin 和 /srv/fireimager/web 中,后者是虚拟目录根目录。当我检测到用户没有在 url 中指定 /index.html 时,它不起作用,没有发出错误,并且 javascript 控制台没有显示任何内容,因此没有任何内容提供给浏览器。
我显然不明白如何使用 VirtualDirectly.serveFiler。
【问题讨论】:
-
有一个类似的问题(虽然有点过时)stackoverflow.com/questions/13081740
-
那个链接帮助了我,但它至少有逻辑错误。