Book Image

Dart By Example

By : David Mitchell
Book Image

Dart By Example

By: David Mitchell

Overview of this book

Table of Contents (17 chapters)
Dart By Example
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

The Hello World server example


It is very simple to create a minimal Hello World server in Dart. Open the sample HelloWorld file of this chapter in the Dart editor. This project is a command-line project, so the main.dart file is placed in the bin folder:

import 'dart:io';
void main() {
  HttpServer.bind('127.0.0.1', 8080).then(
    (server) {
    server.listen((HttpRequest request) {
      request.response
        ..write('Hello Dart World')
        ..close();
    });
  });
}

The output is shown in the following screenshot:

The dart:io package is important for the server side. It allows us access to features that are not available to the code running in the web browser context due to security, such as filesystem and networking.

Launching the server program does not open up a web browser like in our previous application. You have to manually open a browser, then go to the address and to the port that you have bound to. Also, if a change is made in the server code, the application has to be stopped...