有了Dart,我已经拥有了awesome.html,但我希望它能成为现实/awesome.这纯粹是.htaccess(我正在使用Apache)的东西,还是有办法解决这个Dart或"现代网络开发"的方式?
此.htaccess位指示/awesome到/awesome.html:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .*[^/]$ %{REQUEST_URI}/ [L,R=301]
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^(.+)/$ $1.html [L]
Run Code Online (Sandbox Code Playgroud)
但是然后我所有的相对URL引用(对css/js/images)都会中断,如果我将它们从"assets/whatever"重写为"/ assets/whatever",它会在使用Dart编辑器时中断,因为它使用了像:
http://127.0.0.1:3030/Users/dave/Sites/my-dart-app/web/awesome.html
Run Code Online (Sandbox Code Playgroud)
想法?最佳做法?谢谢!
谢谢你的问题!
答案取决于您的 Dart 服务器 VM 前面是否有代理或 Web 服务器。如果您在前面有一个代理,那么代理可以在请求到达您的 Dart 虚拟机之前进行 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);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1039 次 |
| 最近记录: |