shelf_serve 0.2.7

  • Installing
  • Versions
  • 37


Configure and run a http server.

Defining a http server

You define a http server by creating a shelf_serve.yaml file. This file lists the middleware and handlers that should be used to handle requests. For example:

    type: compound
        type: pub
          path: test_web
        type: static
        path: test_static
    type: proxy
    type: rpc
    type: echo


    path: test_api
    path: test_echo

Adding middleware

In the middlewares section of the shelf_serve.yaml file, you can list the middlewares to be used and define optional configuration parameters.

There are two build in middlewares:

  • log_requests logs requests to stdout.
  • cors adds cors headers to the response.


      headers: "rpc-auth, content-type"
      methods: "POST, GET, PUT, OPTIONS, DELETE"
      origin: "*"

Adding handlers

In the handlers section of the shelf_serve.yaml file, you can list different handlers to be used for different routes.

There are five build in handlers:

  • static serves the static files in the directory defined by the path parameter.
  • pub runs pub serve on a dart package and forwards requests to it.
  • proxy proxies requests to the server defined in the url parameter.
  • rpc serves a rest api defined by the rpc package.
  • compound a compound handler that can contain other handlers and middlewares.

Note: when using the rpc handler, you should add the package that defines the api classes in the dependencies section.

Defining custom handlers and middleware

You can define custom handlers and middleware by annotating a factory function that creates the handler or middleware based on some configuration parameters.

For example

import 'package:shelf_serve/shelf_serve.dart';
import 'package:shelf/shelf.dart';

createEchoHandler(String type, String route,
                      Map<String,dynamic> config) {
  return (Request r) async => new Response.ok(await r.readAsString());

You should add the package that defines those custom handlers and middlewares to the dependencies section.

Running the server from command line

First install the shelf_serve executable:

pub global activate shelf_serve

Then goto the directory where the shelf_serve.yaml file is located and run:

shelf_serve serve

By default the server will listen to port 8080. You can define another port by:

shelf_serve serve --port 5000

Running the server from code

Create a dart file and import all the necessary dependencies.

Run the method serve:

serve('path/to/shelf_serve.yaml', port: 5000);

Alternatively, you can run the method serveInIsolate, which will automatically do all the necessary imports based on the dependencies section in the shelf_serve.yaml file.

Creating a docker file

First install the shelf_serve executable:

pub global activate shelf_serve

Then goto the directory where the shelf_serve.yaml file is located and run:

shelf_serve create-docker-project

By default the project will be created in build/project. To select another output directory run:

shelf_serve create-docker-project --output-directory some/other/path

Next, go to the built project directory. There will be a Dockerfile there. You can create a container image by running:

docker build -t my/app .

And to run this image:

docker run -d -p 8080:8080 my/app



  • add gzip middleware


  • api changes
  • create docker project


  • api changes
  • command line utility


  • Initial version

1. Depend on it

Add this to your package's pubspec.yaml file:

  shelf_serve: "^0.2.7"

2. Install it

You can install packages from the command line:

with pub:

$ pub get

Alternatively, your editor might support pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:

import 'package:shelf_serve/shelf_serve.dart';
Version Uploaded Documentation Archive
0.2.7 May 22, 2017 Go to the documentation of shelf_serve 0.2.7 Download shelf_serve 0.2.7 archive
0.2.6 May 22, 2017 Go to the documentation of shelf_serve 0.2.6 Download shelf_serve 0.2.6 archive
0.2.5 Apr 14, 2017 Go to the documentation of shelf_serve 0.2.5 Download shelf_serve 0.2.5 archive
0.2.4 Apr 14, 2017 Go to the documentation of shelf_serve 0.2.4 Download shelf_serve 0.2.4 archive
0.2.3 Oct 3, 2016 Go to the documentation of shelf_serve 0.2.3 Download shelf_serve 0.2.3 archive
0.2.2 Oct 3, 2016 Go to the documentation of shelf_serve 0.2.2 Download shelf_serve 0.2.2 archive
0.2.1 Sep 16, 2016 Go to the documentation of shelf_serve 0.2.1 Download shelf_serve 0.2.1 archive
0.2.0 Sep 15, 2016 Go to the documentation of shelf_serve 0.2.0 Download shelf_serve 0.2.0 archive
0.1.3 Sep 13, 2016 Go to the documentation of shelf_serve 0.1.3 Download shelf_serve 0.1.3 archive
0.1.2 Sep 13, 2016 Go to the documentation of shelf_serve 0.1.2 Download shelf_serve 0.1.2 archive

All 13 versions...


This feature is new.
We welcome feedback.
More details: scoring.

We analyzed this package, and provided a score, details, and suggestions below.

  • tool failures on Feb 19, 2018
  • Dart: 2.0.0-dev.20.0
  • pana: 0.10.1


Describes how popular the package is relative to other packages. [more]
0 / 100
Code health derived from static analysis. [more]
60 / 100
Reflects how tidy and up-to-date the package is. [more]
95 / 100
Overall score:
Weighted score of the above. [more]


Detected platforms: other

Primary library: package:shelf_serve/shelf_serve.dart with components: io, mirrors.


  • Fix lib/src/config.dart.

    Strong-mode analysis of lib/src/config.dart failed with the following error:

    line: 97 col: 52
    The return type 'MiddlewareConfig' isn't a 'HandlerConfig', as defined by anonymous closure.

  • Fix issues reported by dartanalyzer.

    dartanalyzer reported 1 error(s) and 0 warning(s).

  • Package is pre-v1 release.

    While there is nothing inherently wrong with versions of 0.*.*, it usually means that the author is still experimenting with the general direction API.

  • Maintain an example.

    Create a short demo in the example/ directory to show how to use this package. Common file name patterns include: main.dart, example.dart or you could also use shelf_serve.dart.


Package Constraint Resolved Available
Direct dependencies
initialize >=0.6.0+5 <0.7.0 0.6.2+7
pubspec >=0.0.7 <0.1.0 0.0.15
rpc >=0.5.6+2 <0.6.0 0.5.8
shelf >=0.6.5 <0.7.0 0.6.8 0.7.2
shelf_cors >=0.2.1 <0.3.0 0.2.1
shelf_gzip >=1.0.0 <1.1.0 1.0.0
shelf_proxy >=0.1.0+2 <0.2.0 0.1.0+4
shelf_route >=0.14.2 <0.15.0 0.14.3
shelf_rpc >=0.0.3 <0.1.0 0.0.3+2
shelf_static >=0.2.4 <0.3.0 0.2.7
unscripted >=0.6.2 <0.7.0 0.6.2
yaml >=2.1.2 <3.0.0 2.1.13
yamlicious >=0.0.4 <0.1.0 0.0.5
Transitive dependencies
_discoveryapis_commons 0.1.5
analyzer 0.29.5 0.31.1
ansicolor 0.0.9
args 0.13.7 1.3.0
async 1.13.3 2.0.4
barback 0.15.2+14
charcode 1.1.1
cli_util 0.1.2+1
code_transformers 0.5.1+4
collection 1.14.5
concepts 0.2.0
convert 2.0.1
crypto 2.0.2+1
csslib 0.14.1
dart_style 0.2.16 1.0.9+1
discoveryapis_generator 0.9.0 0.9.4
either 0.1.8
gcloud 0.4.0+1
glob 1.1.5
googleapis 0.44.0 0.50.0
googleapis_beta 0.39.2 0.45.0
html 0.13.2+2
http 0.11.3+16
http_parser 3.1.1
isolate 0.2.3 1.1.0
logging 0.11.3+1
matcher 0.12.1+4
meta 1.1.2
mime 0.9.6
mockable_filesystem 0.0.3
option 1.2.0
package_config 1.0.3
path 1.3.9 1.5.1
plugin 0.2.0+2
pool 1.3.4
pub_semver 1.3.2
quiver 0.21.4 0.28.0
quiver_iterables 1.0.1
shelf_path 0.1.8
source_maps 0.10.4
source_span 1.4.0
stack_trace 1.9.2
stream_channel 1.6.3
string_scanner 1.0.2
stuff 0.0.7
supports_color 0.1.1
typed_data 1.1.5
uri 0.11.1
utf 0.9.0+4
watcher 0.9.7+7
Dev dependencies
unittest any