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.