实现一个最简单的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();
}
}