sevr 1.0.3+1

  • Readme
  • Changelog
  • Example
  • Installing
  • 70

A library for building REST APIs easily with Dart modeled after Express JS for Node Js.

The library is still a work in progress and open to contribution.

Created with StageHand - license.

Inspiration #

Our inspiration is the simplicity of express js 👏.

Installing #

Add the following to your pubspec.yaml file:

dependencies:
  sevr: any

Usage #

A simple usage example:

import 'dart:io';

import 'package:sevr/sevr.dart';
import 'package:path/path.dart' as p;

main() {
  var serv = Sevr();

  //let sevr know to serve from the /web directory
  serv.use(Sevr.static('example/web'));

  //Use path to get directory of the files to serve on that route
  serv.get('/serve', [
    (ServRequest req, ServResponse res) {
      return res.status(200).sendFile(p.absolute('example/web/index.html'));
    }
  ]);

  //get request
  serv.get('/test', [
    (ServRequest req, ServResponse res) {
      return res.status(200).json({'status': 'ok'});
    }
  ]);

  //post request
  serv.post('/post', [
    (ServRequest req, ServResponse res) async {
      return res.status(200).json(req.body);
    }
  ]);

  // request parameters
  serv.get('/param/:username', [
    (ServRequest req, ServResponse res) {
      return res.status(200).json({'params': req.params});
    }
  ]);

  // query parameters
  serv.get('/query', [
    (ServRequest req, ServResponse res) {
      return res.status(200).json(req.query);
    }
  ]);

  //Upload Files
  serv.get('/upload', [
    (req, res) async {
      for (var i = 0; i < req.files.keys.length; i++) {
        //Handle your file stream as you see fit, write to file, pipe to a cdn etc --->
        var file = File(req.files[req.files.keys.toList()[i]].filename);
        await for (var data
            in req.files[req.files.keys.toList()[i]].streamController.stream) {
          if (data is String) {
            await file.writeAsString(data, mode: FileMode.append);
          } else {
            await file.writeAsBytes(data, mode: FileMode.append);
          }
        }
      }

      return res.status(200).json(req.body);
    }
  ]);

  //Bind server to port 4000
  serv.listen(4000, callback: () {
    print('Listening on port: ${4000}');
  });
}

Create Server Connection

Pass in the port of your choice in this case: 4000

serv.listen(4000, callback: () {
    print('Listening on port: ${4000}');
  });

Make Server Requests

  • Create requests by passing in the desired route.
  • Put route Controllers in a List of Functions (ServRequest is a helper class that binds to HttpRequest, while ServResponse binds to the response from the HttpRequest Stream).
  • Set response status res.status().

Other available request types:

  • PUT
  • PATCH
  • DELETE
  • COPY
  • HEAD
  • OPTIONS
  • LINK
  • UNLINK
  • PURGE
  • LOCK
  • UNLOCK
  • PROFIND
  • VIEW
serv.get('/test', [
    (ServRequest req, ServResponse res) {
      return res.status(200).json({'status': 'ok'});
    }
  ]);

  serv.post('/post', [
    (ServRequest req, ServResponse res) async {
      return res.status(200).json(req.body);
    }
  ]);

Serve Files From Your Server

  • First Let Sevr know where you want to serve the files from with use() .
  • Here we used the .absolute() function from the path package, pass in the directory of your main file, in this case index.html.
  //let sevr know to serve from the /web directory
  serv.use(Sevr.static('example/web'));

  //Use path to get directory of the files to serve on that route
  serv.get('/serve', [
    (ServRequest req, ServResponse res) {
      return res.status(200).sendFile(p.absolute('example/web/index.html'));
    }
  ]);

Features and bugs #

Please file feature requests and bugs at the issue tracker.

Contributing #

Fork the repo, clone and raise your pull requests against the dev branch, We look forward to your your commits! 😀

1.0.0 #

  • Initial version, get and post Requests.

1.0.2 #

  • More request methods, serving static files from server.

1.0.2+2 #

  • Minor fixes.

1.0.3 #

  • Fixes to serving files, added query parameters feature.

1.0.3+1 #

  • Fix bugs.

example/example.dart

import 'dart:io';

import 'package:sevr/sevr.dart';
import 'package:path/path.dart' as p;

dynamic main() {
  var serv = Sevr();

  //let sevr know to serve from the /web directory
  serv.use(Sevr.static('example/web'));

  //Use path to get directory of the files to serve on that route
  serv.get('/serve', [
    (ServRequest req, ServResponse res) {
      return res.status(200).sendFile(p.absolute('example/web/index.html'));
    }
  ]);

  //get request
  serv.get('/test', [
    (ServRequest req, ServResponse res) {
      return res.status(200).json({'status': 'ok'});
    }
  ]);

  //post request
  serv.post('/post', [
    (ServRequest req, ServResponse res) async {
      return res.status(200).json(req.body);
    }
  ]);

  // request parameters
  serv.get('/param/:username', [
    (ServRequest req, ServResponse res) {
      return res.status(200).json({'params': req.params});
    }
  ]);

  // query parameters
  serv.get('/query', [
    (ServRequest req, ServResponse res) {
      return res.status(200).json(req.query);
    }
  ]);

  //Upload Files
  serv.get('/upload', [
    (req, res) async {
      for (var i = 0; i < req.files.keys.length; i++) {
        //Handle your file stream as you see fit, write to file, pipe to a cdn etc --->
        var file = File(req.files[req.files.keys.toList()[i]].filename);
        await for (var data
            in req.files[req.files.keys.toList()[i]].streamController.stream) {
          if (data is String) {
            await file.writeAsString(data, mode: FileMode.append);
          } else {
            await file.writeAsBytes(data, mode: FileMode.append);
          }
        }
      }

      return res.status(200).json(req.body);
    }
  ]);

  //Bind server to port 4000
  serv.listen(4000, callback: () {
    print('Listening on port: ${4000}');
  });
}

Use this package as a library

1. Depend on it

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


dependencies:
  sevr: ^1.0.3+1

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:sevr/sevr.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
40
Health:
Code health derived from static analysis. [more]
99
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
100
Overall:
Weighted score of the above. [more]
70
Learn more about scoring.

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

  • Dart: 2.7.1
  • pana: 0.13.6

Health suggestions

Fix lib/src/sevr_base.dart. (-1 points)

Analysis of lib/src/sevr_base.dart reported 2 hints:

line 89 col 28: The value of the local variable '_sub' isn't used.

line 398 col 26: The value of the local variable 'data' isn't used.

Format lib/src/build_data.dart.

Run dartfmt to format lib/src/build_data.dart.

Format lib/src/serv_content_types/serv_content_types.dart.

Run dartfmt to format lib/src/serv_content_types/serv_content_types.dart.

Format lib/src/serv_request_response_wrapper/serv_request_wrapper.dart.

Run dartfmt to format lib/src/serv_request_response_wrapper/serv_request_wrapper.dart.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.5.0 <3.0.0
body_parser ^1.1.1 1.1.1
dart2_constant ^1.0.2+dart2 1.0.2+dart2
mime ^0.9.6+3 0.9.6+3
path ^1.6.0 1.6.4
pedantic ^1.8.0 1.9.0
Transitive dependencies
charcode 1.1.3
collection 1.14.12
http_parser 3.1.4
http_server 0.9.8+3
meta 1.1.8
source_span 1.7.0
string_scanner 1.0.5
term_glyph 1.1.0
typed_data 1.1.6
Dev dependencies
test ^1.6.0