使用shelf_static服务通过飞镖静态网页是没有问题的:
var staticHandler = createStaticHandler(staticPath, defaultDocument:'home.html');
io.serve(staticHandler, 'localhost', port).then((server) {
print('Serving at http://${server.address.host}:${server.port}');
});
Run Code Online (Sandbox Code Playgroud)
我可以shelf_route很好地用于动态网页:
Router routes = new Router()
..get('/item/{itemid}', handler.doItem);
var handler = const shelf.Pipeline()
.addHandler(routes.handler);
io.serve(handler, 'localhost', port).then((server) {
print('Serving at http://${server.address.host}:${server.port}');
});
Run Code Online (Sandbox Code Playgroud)
但我正在努力为动态版本添加静态处理程序.我尝试过的事情包括:
Router routes = new Router()
..get('/item/{itemid}', handler.doItem)
..get('/', staticHandler);
Run Code Online (Sandbox Code Playgroud)
要么 ...
..get('/.*', staticHandler);
Run Code Online (Sandbox Code Playgroud)
要么 ...
..get('/{any}', staticHandler);
Run Code Online (Sandbox Code Playgroud)
home.html如果我请求http://localhost:8080/但明确要求现有页面http://localhost:8080/home.html,所有这些都给我指定的默认页面给我Not Found.
我甚至不想尝试这样做shelf_static吗?如果没有,那么正确的方法是什么?谢谢!
你可以用Cascade.它创建了一系列处理程序,如果前一个处理程序给出404或405响应,则移动到下一个处理程序.
var staticHandler = createStaticHandler(staticPath, defaultDocument:'home.html');
var routes = new Router()
..get('/item/{itemid}', handleItem);
var handler = new Cascade()
.add(staticHandler)
.add(routes.hander)
.handler;
io.serve(handler, 'localhost', port).then((server) {
print('Serving at http://${server.address.host}:${server.port}');
});
Run Code Online (Sandbox Code Playgroud)
原因是shelf_route像这样的方法get必须完全匹配路径.对于静态文件,您不需要完全匹配,因为路径的其余部分会告诉您文件的路径.
为此,您需要使用add方法,并设置exactMatch: false为当前类的方法get,post等不暴露exactMatch.
以下作品
void main(List<String> args) {
Logger.root.onRecord.listen(print);
var staticHandler = createStaticHandler('../static', defaultDocument:'home.html');
final root = router()
..get('/item/{itemid}', (Request request) => 'handling the item')
..add('/', ['GET'], staticHandler, exactMatch: false);
printRoutes(root);
io.serve(root.handler, InternetAddress.ANY_IP_V6, 9999);
}
Run Code Online (Sandbox Code Playgroud)
仅供参考我添加了一个名为mojito的更高级别框架,它是许多架子组件上的薄胶层,这使得它更容易一些.
它仍然有点新鲜且记录不清,但如果您感兴趣,可以执行以下操作
void main(List<String> args) {
Logger.root.onRecord.listen(print);
final app = mojito.init();
app.router
..get('/item/{itemid}', (String itemid) => 'handling the item $itemid')
..addStaticAssetHandler('/', fileSystemPath: '../static',
defaultDocument:'home.html');
app.start();
}
Run Code Online (Sandbox Code Playgroud)
addStaticAssetHandler呼吁createStaticHandler在幕后,但也支持调用酒馆服务于发展模式,这是非常方便的对像聚合物的东西
| 归档时间: |
|
| 查看次数: |
1158 次 |
| 最近记录: |