DEV Community

Cover image for Quick Tip: Write an HTTP server in Dart 💻
Jermaine
Jermaine

Posted on • Edited on • Originally published at creativebracket.com

5 1

Quick Tip: Write an HTTP server in Dart 💻

In today's quick tip, we will explore one of the inbuilt classes Dart gives us for creating web servers. This class comes as part of the "dart:io" library in the SDK.


The class in particular is appropriately named HttpServer. Here's how we'll use it:

HttpServer.bind("localhost", 8080).then((HttpServer server) { ... });
Enter fullscreen mode Exit fullscreen mode

bind() represents a static method that takes as required arguments a hostname and a port. This returns a Future<HttpServer>, allowing us to chain on methods like then() and catch().

A successfully bound hostname and port now allows us to receive incoming requests by calling the listen() method on the server object, which has a Streaming interface:

// ...
server.listen((HttpRequest request) {
  request.response.write('Hello world');
  request.response.close();
});
Enter fullscreen mode Exit fullscreen mode

Receiving the request allows us to write out our response and end it. Here's the full snippet in its glory:

import 'dart:io';

void main() {
  HttpServer
    .bind("localhost", 8080)
    .then((HttpServer server) {
      server.listen((HttpRequest request) {
        request.response.write("Hello world!");
        request.response.close();
      });
    });
}
Enter fullscreen mode Exit fullscreen mode

Using Async/Await 🔥

Here's a prettier way of writing a basic server:


import 'dart:io';

main() async {
  var server = await HttpServer.bind("localhost", 8080);

  await for (var request in server) {
    request.response.write("Hello world");
    request.response.close();
  }
}
Enter fullscreen mode Exit fullscreen mode

Here's a video demonstrating the first example:

Hope this was insightful.

Like, share and follow me for more content on Dart.

Quick links

  1. HttpServer class
  2. Free Dart screencasts on Egghead.io

Image of Timescale

PostgreSQL for Agentic AI — Build Autonomous Apps on One Stack 1️⃣

pgai turns PostgreSQL into an AI-native database for building RAG pipelines and intelligent agents. Run vector search, embeddings, and LLMs—all in SQL

Build Today

Top comments (0)

Sentry growth stunted Image

If you are wasting time trying to track down the cause of a crash, it’s time for a better solution. Get your crash rates to zero (or close to zero as possible) with less time and effort.

Try Sentry for more visibility into crashes, better workflow tools, and customizable alerts and reporting.

Switch Tools 🔁

👋 Kindness is contagious

Engage with a wealth of insights in this thoughtful article, valued within the supportive DEV Community. Coders of every background are welcome to join in and add to our collective wisdom.

A sincere "thank you" often brightens someone’s day. Share your gratitude in the comments below!

On DEV, the act of sharing knowledge eases our journey and fortifies our community ties. Found value in this? A quick thank you to the author can make a significant impact.

Okay