Dart Shelf server tutorial

6 min readJan 5, 2024

Getting started with your first Shelf web server


If this is your first time using the shelf_router package, the documentation can be a little difficult to follow. Creative Bracket has a good video tutorial here, but this article will provide a written guide to get you started.

Other alternatives to Shelf include Dart Frog, Serverpod, Globe, or just plain Dart. Plain Dart is a little too low-level for me, and I’m still waiting for the other frameworks to establish themselves a little longer. I was burned when Aqueduct stopped being maintained, so I’d like something that I know will be stable for years. Shelf is maintained by the Dart team and its API hasn’t seemed to change much over the years.

Creating a new project

Create a new Dart server project. You can change my_shelf_server to whatever you want to call your project:

dart create -t server-shelf my_shelf_server

The -t argument is for selecting templates. In this case, the template is server-shelf. This will add the shelf and shelf_router packages to your project along with some sample server boilerplate code.

Exploring your project

Open the project and look at the file structure:

Assuming you’re already familiar with a standard Dart project, here are the files that are of interest related to the server:

  • server.dart: This is where your server code resides. The default implementation starts the server, adds a logger, and handles requests. As your project becomes bigger, you can create a lib folder in the root of the project and move most of your logic there.
  • server_test.dart: It’s pretty straightforward to make a request and test that the response matches the expected output.
  • Dockerfile: Docker is one method of deploying your server. A Docker image contains all of the dependencies your Dart server needs. The Dockerfile is a text file with instructions on how to build the image.
  • .dockerignore: Similar to .gitignore for Git…