实现一个最简单的HTTP服务器

1738次阅读  |  发布于5年以前

实现一个最简单的HTTP服务器

Use HttpServer.bind() method to bind to a port. The await for listens for connections. Respond to an HttpRequest using the response property.

import 'dart:io';

main() async {
  var server = await HttpServer.bind(InternetAddress.LOOPBACK_IP_V4, 8080);
  print("Serving at ${server.address}:${server.port}");
  await for (HttpRequest request in server) {
    request.response
      ..headers.contentType = new ContentType("text", "plain", charset: "utf-8")
      ..write('Hello, world')
      ..close();
  }
}

Copyright© 2013-2020

All Rights Reserved 京ICP备2023019179号-8