pathrouter 0.1.2

  • Readme
  • Changelog
  • Example
  • Installing
  • 54

PathRouter #

PathRouter is a middleware plugin that can be used with shelf to provide a means of assigning a handler, or a router as defined by shelf_router, to a named path.

For example you may want a set of handlers to deal with your /api route, or maybe a handler to deal with your /users route. Using PathRouter you can create seperate routers/handlers and assign then to handle specific paths.

Usage #

PathRouter Component #

Use this to associate a middleware handler with a named route, for example if you want a handler (myHandler) to process all requests to the route /api you would declare something similar to

  PathRouter().router("/api", myHandler);

PathRouter now also has a catchAll route that can be used to add a handler to any other routes not taken care of. This should always be the last middleware definition.

PathRouter().catchAll(myHandler);

Note that if a route to /api was requested and not handled by the defined PathRouter then it would be handled by any innerHandler set up,( with .addhandler() ) unless the optional error handler (myErrorHandler) was also defined.

  PathRouter().router("/api", myHandler, errorHandler: myErrorHandler);

PathRouterTable #

This is a just a convienence component to help you define a specific routing table to use with PathRouter that allows you to quickly create and manage multiple results to various path requests. From the examples above you could use it to define what happens to various paths below the /api route. Note that the syntax for defining these routes is the same as when defining your own shelf_router handler as it is just a wrapper around this process.

class APIRouter extends PathRouterTable {
  @override
  defineTable(){
    app.get('/user', (Request request) {
      return Response.ok('api user request');
    });

    app.get('/status', (Request request) {
      return Response.ok('api status request');
    });    
  }
}

All you need to do is put all your route handlers in the overridden defineTable() method. The PathRouter call would then become:

PathRouter().router("/api", APIRouter().handler, errorHandler: myErrorHandler);

This would then handle:

  • /api/users
  • /api/status

see the example directory for a simple but complete example of how to use PathRouter and PathRouterTable

Info #

Created from templates made available by Stagehand under a BSD-style license.

0.1.2 #

  • added a catchAll option to the PathRouter. Added check to see if there is a path before checking for matching route

0.1.1 #

  • updated the PathRouterTable to accept an option Map. This allows data to be passed in

0.1.0 #

  • Initial release

example/README.md

An example web server using PathRouter

Use this package as a library

1. Depend on it

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


dependencies:
  pathrouter: ^0.1.2

2. Install it

You can install packages from the command line:

with pub:


$ pub get

with Flutter:


$ flutter pub get

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

3. Import it

Now in your Dart code, you can use:


import 'package:pathrouter/pathrouter.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
8
Health:
Code health derived from static analysis. [more]
100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
100
Overall:
Weighted score of the above. [more]
54
Learn more about scoring.

We analyzed this package on Jul 11, 2020, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.8.4
  • pana: 0.13.15

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.1.0 <3.0.0
shelf ^0.7.2 0.7.7
shelf_router ^0.7.2 0.7.2
Transitive dependencies
async 2.4.2
charcode 1.1.3
collection 1.14.13 1.15.0-nullsafety
http_methods 1.0.0
http_parser 3.1.4
meta 1.2.2 1.3.0-nullsafety
path 1.7.0
source_span 1.7.0
stack_trace 1.9.5
stream_channel 2.0.0
string_scanner 1.0.5
term_glyph 1.1.0
typed_data 1.2.0 1.3.0-nullsafety
Dev dependencies
pedantic ^1.0.0
test ^1.0.0