Book Image

Mastering Dart

By : Sergey Akopkokhyants
Book Image

Mastering Dart

By: Sergey Akopkokhyants

Overview of this book

Table of Contents (19 chapters)
Mastering Dart
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Securing a server


Open the server.dart file and type the following lines:

import 'dart:io';

main() {
  var pkcertDB = Platform.script.resolve('pkcert').toFilePath();
  SecureSocket.initialize(database: pkcertDB,
    password: 'changeit');

  HttpServer
  .bindSecure(InternetAddress.ANY_IP_V6, 8443,
    certificateName: 'localhost_cert')
  .then((server) {
    server.listen((HttpRequest request) {
      request.response.write('Hello, world!');
      request.response.close();
    });
  });
}

This is an implementation of the well-known Hello, World! example. I always keep the password of my certificate in the code only for demonstration purposes. Please keep your password in an external encrypted file. The code of the server is pretty straightforward. One small exception is that it references SecureSocket instead of the Socket class. By calling a static initialize method of this class, we initialize the NSS library. Now, we should organize binding with the static bindSecure method of HttpServer...