O'Reilly logo

Dart: Scalable Application Development by Ivo Balbaert, Sergey Akopkokhyants, Davy Mitchell

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Creating a web server

The class HttpServer is used to write web servers; this server listens on (or binds to) a particular host and port for incoming HTTP requests. It provides event handlers (better called request handlers in this case) that are triggered when a request with incoming data from a web client is received.

How to do it...

We make a project called simple_webserver starting from the template command-line application and import dart:io as follows:

import 'dart:io'; //Define host and port: InternetAddress HOST = InternetAddress.LOOPBACK_IP_V6; const int PORT = 8080; main() { // Starting the web server: HttpServer.bind(HOST, PORT) .then((server) { print('server starts listening on port ${server.port}'); // Starting the request handler: server.listen(handleRequest); ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required