Dart by Example: Http Server

  1. import 'dart:io';
  2. main() async {
  3. var server = await HttpServer.bind(InternetAddress.ANY_IP_V4, 8777);
  4. print('serving on port ${server.port}');
  5. // HttpServer extends Stream, so using await-for
  6. // will run the loop body when a request is added to the stream.
  7. await for (HttpRequest req in server) {
  8. // resposne
  9. req.response
  10. ..write("welcome to my dart server")
  11. ..close();
  12. }
  13. }
  14.  
  1. $ dart http_server.dart
  2. serving on port 8777

opening localhost:8777:

  1. welcome to my dart server

by @jryanio | source | license