• Stars
    star
    457
  • Rank 92,222 (Top 2 %)
  • Language
    Dart
  • Created over 7 years ago
  • Updated about 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Jaguar, a server framework built for speed, simplicity and extensible. ORM, Session, Authentication & Authorization, OAuth

Pub Build Status Gitter

Jaguar

Jaguar is a full-stack production ready HTTP server framework built to be fast, simple and intuitive.

Getting started

Familiar way to write routes

Jaguar class provides methods get, put, post, delete and options to quickly add route handlers for specific HTTP methods.

main() async {
  final server = Jaguar();  // Serves the API at localhost:8080 by default
  // Add a route handler for 'GET' method at path '/hello'
  server.get('/hello', (Context ctx) => 'Hello world!');
  await server.serve();
}

Powerful route matching

Easily define and access Path parameters

Path segments prefixed with : can match any value and are also captured as path variables. Path variables can be accessed using pathParams member of Context object.

main(List<String> args) async {
  final quotes = <String>[
    'But man is not made for defeat. A man can be destroyed but not defeated.',
    'When you reach the end of your rope, tie a knot in it and hang on.',
    'Learning never exhausts the mind.',
  ];

  final server = Jaguar();
  server.get('/api/quote/:index', (ctx) { // The magic!
    final int index = ctx.pathParams.getInt('index', 1);  // The magic!
    return quotes[index + 1];
  });
  await server.serve();
}
  • A path can have multiple path variables.
  • A path variable can appear at any position in the path.
  • A path variable can be matched against a Regular expression.
  • getInt, getDouble, getNum and getBool methods can be used to easily typecast path variables.
  • Using * as the final path segment captures/matches all following segments.

Easily access Query parameters

Query parameters can be accessed using queryParams member of Context object.

main(List<String> args) async {
  final quotes = <String>[
    'But man is not made for defeat. A man can be destroyed but not defeated.',
    'When you reach the end of your rope, tie a knot in it and hang on.',
    'Learning never exhausts the mind.',
  ];

  final server = Jaguar();
  server.get('/api/quote', (ctx) {
    final int index = ctx.queryParams.getInt('index', 1); // The magic!
    return quotes[index + 1];
  });
  await server.serve();
}

getInt, getDouble, getNum and getBool methods can be used to easily typecast query parameters into desired type.

One liner to access Forms

A single line is all it takes to obtain a form as a Map<String, String> using method bodyAsUrlEncodedForm on Request object.

main(List<String> arguments) async {
  final server = Jaguar(port: 8005);

  server.postJson('/api/add', (ctx) async {
      final Map<String, String> map = await ctx.req.bodyAsUrlEncodedForm(); // The magic!
      contacts.add(Contact.create(map));
      return contacts.map((ct) => ct.toMap).toList();
    });


  await server.serve();
}

One liner to serve static files

The method staticFiles adds static files to Jaguar server. The first argument determines the request Uri that much be matched and the second argument determines the directory from which the target files are fetched.

main() async {
  final server = Jaguar();
  server.staticFiles('/static/*', 'static'); // The magic!
  await server.serve();
}

JSON serialization with little effort

Decoding JSON requests can't be simpler than using one of the built-in bodyAsJson, bodyAsJsonMap or bodyAsJsonList methods on Request object.

Future<void> main(List<String> args) async {
  final server = Jaguar();
  server.postJson('/api/book', (Context ctx) async {
    // Decode request body as JSON Map
    final Map<String, dynamic> json = await ctx.req.bodyAsJsonMap();
    Book book = Book.fromMap(json);
    return book; // Automatically encodes Book to JSON
  });

  await server.serve();
}

Out-of-the-box Sessions support

main() async {
  final server = Jaguar();
  server.get('/api/add/:item', (ctx) async {
    final Session session = await ctx.req.session;
    final String newItem = ctx.pathParams.item;

    final List<String> items = (session['items'] ?? '').split(',');

    // Add item to shopping cart stored on session
    if (!items.contains(newItem)) {
      items.add(newItem);
      session['items'] = items.join(',');
    }

    return Response.redirect('/');
  });
  server.get('/api/remove/:item', (ctx) async {
    final Session session = await ctx.req.session;
    final String newItem = ctx.pathParams.item;

    final List<String> items = (session['items'] ?? '').split(',');

    // Remove item from shopping cart stored on session
    if (items.contains(newItem)) {
      items.remove(newItem);
      session['items'] = items.join(',');
    }

    return Response.redirect('/');
  });
  await server.serve();
}

More Repositories

1

jaguar_orm

Source-generated ORM with relations (one-to-one, one-to-many, many-to-many), preloading, cascading, polymorphic relations, etc
Dart
218
star
2

jaguar_serializer

Format (JSON, XML, protobuf, mongodb, etc) and platform (server, client) agnostic serialization framework
Dart
172
star
3

client

Contains various packages for client side
Dart
46
star
4

jaguar_flutter_asset

Serve files from Flutter assets.
Dart
25
star
5

observable_ish

Observable state and events for browser and Flutter.
Dart
24
star
6

serve

Serves static files in a directory
Dart
11
star
7

nuts

Nuts and bolts for building cross-platform UI (HTML, Flutter, CLI) using Dart. Also screw frameworks (React, Vue, Angular).
Dart
11
star
8

session

Session support for Jaguar
Dart
11
star
9

cqrs

Command Query Responsibility Separation framework for Dart
Dart
9
star
10

jaguar_hotreload

Hot reloader for Dart
Dart
9
star
11

stencil

A super simple type-safe, analyzer and code-completion friendly HTML template library for Dart
Dart
7
star
12

jaguar_validate

A simple, source generated Validator framework
Dart
5
star
13

jaguar_json

jaguar_serializer based JSON interceptors for Jaguar
Dart
4
star
14

jaguar_serializer_protobuf

Google Protocol buffer support for Jaguar serializer
Dart
3
star
15

data_store

DB agnostic abstraction of CRUD over a specific model/document/table.
Dart
2
star
16

cache

Cache layer for Jaguar
Dart
2
star
17

jaguar_rpc

A simple JSON based RPC protocol
Dart
2
star
18

logger

Level logger with backends for file, http and postgres with support for log rotation
Dart
2
star
19

auth_header

Utility library to parse and manipulate HTTP Authorisation header
Dart
2
star
20

db

Database middleware and utilities for Jaguar
Dart
2
star
21

jaguar_cli

CLI for jaguar-dart
Dart
1
star
22

jaguar_serializer_mongo

Mongo serializer helpers for jaguar_serializer
Dart
1
star
23

jaguar_recaptcha

reCAPTCHA server side support Dart and Jaguar.dart
Dart
1
star
24

jaguar_facebook_client

Facebook client for Dart from Jaguar authors
Dart
1
star