Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Simple, minimal, and efficient Dart web developement framework.
Dart
Branch: master

Fetching latest commit…

Cannot retrieve the latest commit at this time

Failed to load latest commit information.
example
lib
resources
test
.gitignore
README.md
pubspec.lock
pubspec.yaml

README.md

synth!

synth

Simple, minimal, and efficient Dart web developement framework.

Prerequisite: Dart and Git installed in your machine.

Basic usage

Step 1: Create a project using Dart IDE or commandline. Instructions below is thru commandline.

$ mkdir hello
$ cd hello

Step 2: Inside hello directory create a pubspec.yaml file with the contents below.

name: hello
dependencies:
  synth:
    git: git://github.com/maiah/synth.git

Step 3: Inside hello directory execute pub install. This will create packages folder and download the synth library.

$ pub install

Step 4: Create hello_server.dart file inside hello folder and import synth.dart library in that file.

import 'package:synth/synth.dart';

Step 5: And also in this file create main method and define your routes and start the HTTP server.

main () {
  route('GET', '/', (req, res)
    => res.write('Hello, World!'));

  start(port: 7000);
  print('Listening on port 7000');
}

Step 6: Run your Dart program using Dart IDE or commandline. Instructions below is thru commandline.

$ dart hello_server.dart

Step 7: Open your web browser and go to http://localhost:7000 and the message below will be shown.

Hello, World!

Routing

You can register a route with a path that holds a variable like below.

route('GET', '/person/:name', (req, res)
  => res.write('Hi there.'));

Then you can access this route with http://localhost:7000/person/maiah URL.

You can also have multiple variable in a single route path.

route('GET', '/person/:name/department/:id', (req, res)
  => res.write('Hello there.'));

Then you can access this route with http://localhost:7000/person/maiah/department/557 URL.

Adding middleware

Adding middleware is very simple. It's like providing a HTTP request handler. But you have to register your middleware thru the use method.

Unlike request handler, middlewares has a 3rd parameter next that can be executed to call the next middleware in the stack. Take a look at the typedef Middleware signature below:

typedef void Middleware(Request req, Response res, next);

For example you want to add a middleware that will log the request path each time a request is processed.

use((req, res, next) {
  print('Request path is ${req.path}');
  next(); // Executes the next middleware in the stack if any.
});

The code above will register the middleware closure you created and will execute everytime a request is processed.

Adding middleware into a specific route

Adding middleware into a specific route is done thru route method. The middleware method signature is the same. Add it before the request handler like below.

...
var someMiddleware = (req, res, next) {
  print('Some middleware here.');
  next();
}
...

route('GET', '/', someMiddleware, (req, res) {
  res.write('A route with middleware.');
});

The code above will register and execute the middleware only for this specific route.

Built-in middlewares

  • logPath - Used for logging the request path and its query parameters.
  • reqContent - Used to gather request POST data and populate Request#dataMap property which can be accessed by user-defined request handlers and other middlewares.
  • reqJsonContent - Similar to reqContent, except it parses JSON POST data and populates Request#dataObj.

Synth Robot Boy art by KabisCube kabiscube@yahoo.com

Something went wrong with that request. Please try again.