如何使用Dart http_server:VirtualDirectory [英] How to use Dart http_server:VirtualDirectory

查看:305
本文介绍了如何使用Dart http_server:VirtualDirectory的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直使用 dart:route api来提供静态文件,但我注意到有一个核心库 http_server 包含 dart:io HttpServer 的帮助类和函数。

I have been using the dart:route api for serving static files but I noticed that there is a core library called http_server that contains helper classes and functions for dart:io HttpServer.

我特别感兴趣的是类 VirtualDirectory ,根据docs,静态内容的目录,然后你调用的方法 serve()

Of particular interest to me is the class VirtualDirectory which, according to the docs, takes a String Object for the static content of directory and then you call the method serve()

var virtualDirectory = new VirtualDirectory('/var/www/');
virtualDirectory.serve(new HttpServer('0.0.0.0', 8080));

这不起作用,因为没有HttpServer的构造函数 - 至少在当前版本中。

This doesn't work as there is no constructor for HttpServer - at least not in current versions.

virtualDirectory.serve(HttpServer.bind('0.0.0.0', 8080));

这是我如何实例化服务器也失败,因为 virtualDirectory.serve ()不接受未来< HttpServer> ,最后:

Which is how I have been instantiating a server also fails since virtualDirectory.serve() doesn't take a Future<HttpServer> and finally:

virtualDirectory.serve(HttpServer.bind('0.0.0.0', 8080).asStream());

也会失败,
参数类型'Stream'不能分配给参数类型
'Stream'

also fails with The argument type 'Stream' cannot be assigned to the parameter type 'Stream'

那么如何将VirtualDirectory连接到服务器?没有我可以在线找到的示例,VirtualDirectory源代码没有说明。我会RTFM如果我可以FTFM。

So how do I connect a VirtualDirectory to a Server? There are no examples that I can find online and the VirtualDirectory source code does not make it clear. I would RTFM if I could FTFM. Links are fine as answers.

推荐答案

VirtualDirectory 未来由 HttpServer.bind 返回。您可以使用以下五行代码创建静态文件Web服务器:

The VirtualDirectory can work from inside the Future returned by HttpServer.bind. You can create a static file web server by using the following five lines of code:

HttpServer.bind('127.0.0.1', 8888).then((HttpServer server) {
    VirtualDirectory vd = new VirtualDirectory('../web/');
    vd.jailRoot = false;
    vd.serve(server);
});

您可以通过在提交文件之前解析URI和提取服务请求,使其更复杂。

You can make it more sophisticated by parsing the URI and pulling out service requests prior to serving out files.

import 'dart:io';
import 'package:http_server/http_server.dart';

main() {

  handleService(HttpRequest request) {
    print('New service request');
    request.response.write('[{"field":"value"}]');
    request.response.close();
  };

  HttpServer.bind('127.0.0.1', 8888).then((HttpServer server) {
    VirtualDirectory vd = new VirtualDirectory('../web/');
    vd.jailRoot = false;
    server.listen((request) { 
      print("request.uri.path: " + request.uri.path);
      if (request.uri.path == '/services') {
        handleService(request);
      } else {
        print('File request');
        vd.serveRequest(request);
      } 
    });
  });
}

这篇关于如何使用Dart http_server:VirtualDirectory的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆