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

Quickstart image

Django MongoDB Backend Quickstart! A Step-by-Step Tutorial

Get up and running with the new Django MongoDB Backend Python library! This tutorial covers creating a Django application, connecting it to MongoDB Atlas, performing CRUD operations, and configuring the Django admin for MongoDB.

Watch full video →

Top comments (0)

ACI image

ACI.dev: The Only MCP Server Your AI Agents Need

ACI.dev’s open-source tool-use platform and Unified MCP Server turns 600+ functions into two simple MCP tools on one server—search and execute. Comes with multi-tenant auth and natural-language permission scopes. 100% open-source under Apache 2.0.

Star our GitHub!

đź‘‹ Kindness is contagious

Dive into this informative piece, backed by our vibrant DEV Community

Whether you’re a novice or a pro, your perspective enriches our collective insight.

A simple “thank you” can lift someone’s spirits—share your gratitude in the comments!

On DEV, the power of shared knowledge paves a smoother path and tightens our community ties. Found value here? A quick thanks to the author makes a big impact.

Okay