谢谢你的提问!
答案取决于您的 Dart 服务器虚拟机前面是否有代理或 Web 服务器。如果你前面有一个代理,那么代理可以在请求到达你的 Dart VM 之前进行 URL 重写。无论如何,这是一个不错的场景,因为代理可以进行缓存、SSL、负载平衡等等。在这种情况下,Dart VM 只是一个“应用服务器”。作为最佳实践,我建议在前面放置一个工业级 Web 服务器或代理。
但是,如果你想纯粹在 Dart 中进行 URL 屏蔽和重写,这里有一些代码。正如 Kai 在上面的评论中所说,这通常是框架的工作。但无论如何我都会在这里包含一些代码来娱乐。:)
import 'dart:io';
import 'dart:json';
class StaticFileHandler {
final String basePath;
StaticFileHandler(this.basePath);
_send404(HttpResponse response) {
response.statusCode = HttpStatus.NOT_FOUND;
response.outputStream.close();
}
String rewritePath(String path) {
String newPath = path;
if (path == '/' || path.endsWith('/')) {
newPath = '${path}index.html';
} else if (!path.endsWith('.html')) {
newPath = "${path}.html";
}
return newPath;
}
// TODO: etags, last-modified-since support
onRequest(HttpRequest request, HttpResponse response) {
String path = rewritePath(request.path);
final File file = new File('${basePath}${path}');
file.exists().then((found) {
if (found) {
file.fullPath().then((String fullPath) {
if (!fullPath.startsWith(basePath)) {
_send404(response);
} else {
file.openInputStream().pipe(response.outputStream);
}
});
} else {
_send404(response);
}
});
}
}
runServer(String basePath, int port) {
HttpServer server = new HttpServer();
server.defaultRequestHandler = new StaticFileHandler(basePath).onRequest;
server.onError = (error) => print(error);
server.listen('127.0.0.1', 1337);
print('listening for connections on $port');
}
main() {
var script = new File(new Options().script);
var directory = script.directorySync();
runServer("${directory.path}", 1337);
}