Dart HTTPS Server

In my previous post I used a simple HTTPS server written in Node.js and I was curious how that would look like in Dart. And it’s very short too:

import 'dart:io';

Future<void> main() async {
  var chain = Platform.script.resolve('cert.pem').toFilePath();
  var key = Platform.script.resolve('key.pem').toFilePath();
  var context = SecurityContext()
    ..useCertificateChain(chain)
    ..usePrivateKey(key);
  var server = await HttpServer.bindSecure(InternetAddress.anyIPv4, 8080, context);
  await server.forEach((HttpRequest request) {
    print("${request.method} ${request.uri.path}");
    print(request.headers);
    request.response.close();
  });
}

Works just as well as the Node.js counterpart.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.