Angel
1.x
1.x
  • Introduction
  • Example Projects
  • Awesome Angel
  • 1.1.0 Migration Guide
  • Social
    • Angel on Gitter
    • Angel on Medium
    • Angel on YouTube
  • The Basics
    • Installation & Setup
      • Without the Boilerplate
    • Requests & Responses
    • Dependency Injection
    • Basic Routing
    • Request Lifecycle
    • Middleware
    • Controllers
    • Handling File Uploads
    • Using Plug-ins
    • Rendering Views
    • REST Client
    • Testing
    • Error Handling
    • Pattern Matching and Parameter
    • Command Line
  • Flutter
    • Writing a Chat App
    • Flutter helper widgets
  • Services
    • Service Basics
    • TypedService
    • In-Memory
    • Custom Services
    • Hooks
      • Bundled Hooks
    • Database-Agnostic Relations
    • Database Adapters
      • MongoDB
      • RethinkDB
      • JSON File-based
  • Plug-ins
    • Authentication
    • Configuration
    • Diagnostics & Logging
    • Reverse Proxy
    • Service Seeder
    • Static Files
    • Validation
    • Websockets
    • Server-sent Events
    • Toggle-able Services
  • Middleware/Finalizers
    • CORS
    • Response Compression
    • Security
    • File Upload Security
    • shelf Integration
    • User Agents
    • Pagination
    • Range, If-Range, Accept-Ranges support
  • PostgreSQL ORM
    • Model Serialization
    • Query Builder + ORM
    • Migrations
  • Deployment
    • Running in Isolates
    • Configuring SSL
    • HTTP/2 Support
    • Ubuntu and nginx
    • AppEngine
    • Production Mode
  • Front-end
    • Mustache Templates
    • Jael template engine
      • Github
      • Basics
      • Custom Elements
      • Strict Resolution
      • Directive: declare
      • Directive: for-each
      • Directive: extend
      • Directive: if
      • Directive: include
      • Directive: switch
    • compiled_mustache-based engine
    • html_builder-based engine
    • Markdown template engine
    • Using Angel with Angular
  • Advanced
    • API Documentation
    • Contribute to Angel
    • Scaling & Load Balancing
    • Standalone Router
    • Writing a Plugin
    • Task Engine
    • Hot Reloading
    • Real-time polling
Powered by GitBook
On this page
  • Routing
  • Route Parameters
  • RegExp Routes
  • Sub-Apps
  • Route Groups
  • Extended Documentation
  • Next Up...
  1. The Basics

Basic Routing

PreviousDependency InjectionNextRequest Lifecycle

Last updated 6 years ago

Routing

There is only one method responsible for adding routes to your application:

app.addRoute('<method>', '<path>', requestHandler);

However, the following methods are available for convenience, and are the ones you will use most often. Each method's name responds to an HTTP request method. For example, a route declared with app.get(...), will respond to HTTP GET requests.

app.get('<path>', requestHandler);
app.post('<path>', requestHandler);
app.patch('<path>', requestHandler);
app.delete('<path>', requestHandler);

Your requestHandler can be any Dart value, whether a function, or an object. See the pages for detailed documentation.

Route paths do not have to begin with a forward slash, as leading and trailing slashes are stripped from route paths internally.

Route Parameters

Say you're building an API, or an MVC application. You typically want to serve the same view template on multiple paths, corresponding to different ID's. You can do this as follows, and all parameters will be available via req.params:

app.get('/todos/:id', (RequestContext req, res) async => {'id': req.params['id']});

Remember, route parameters must be preceded by a colon (':'). Parameter names must start with a letter or underscore, optionally followed by letters, underscores, or numbers. Parameters will match any character except a forward slash ('/') in a request URI.

Examples:

  • :id

  • :_hello

  • :param123

RegExp Routes

You can also use a RegExp as a route pattern, but you may have to parse the URI yourself, if you need to access specific parameters.

app.post(new RegExp(r'\/todos/([A-Za-z0-9]+)'), (req, res) async => "RegExp");

Route parameters can also have custom regular expressions, to remove the requirement of manual parsing. Simply enclose the regular expression in a set of parentheses following the parameter's name.

app.get(r'/number/:num([0-9]+(\.[0-9])?)', ...);

Sub-Apps

You can mount routers, or use entire sub-apps.

Angel app = new Angel();
app.get('/', 'Hello!');

var subRouter = new Router()..get('/', 'Subroute');
app.mount('/sub', subApp);
// Now, you can visit /sub and receive the message "Subroute"

var subApp = new Angel()..get('/hello', 'world');
app.use('/api', subApp);

// GET /api/hello returns "world"

Route Groups

Routes can also be grouped together. Route parameters will be applied to sub-routes automatically. Route groups can be nested as well.

app.group('/user/:id', (router) {
  router.get('/messages', (String id) => fetchUserMessages(id));
  router.group('/nested', ...);
});

Extended Documentation

Next Up...

For more documentation on the router, see . has no dart:io or dart:mirrors dependency, and it also supports browser use (both hash and push state).

Learn how let you reuse functionality across your entire routing setup.

its repository
package:angel_route
middleware
Routing
Route Parameters
RegExp Routes
Mounting and Sub-Apps
Route Groups
Extended Documentation
Next Up...
Requests and Responses