sevr 1.0.2+3

  • Readme
  • Changelog
  • Example
  • Installing
  • 69

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 👏.

Usage #

A simple usage example:

import 'package:sevr/sevr.dart';

main() {
  var serv = Sevr();

  //first notify **sevr** of your directory of static files
  serv.use(Sevr.static('./web'));

  //create controller,middleware classes etc, put them in a list and pass them into the router methods

  //serve files from the port
  serv.get('/file', [
    (ServRequest req, ServResponse res) {
      return res.status(200).sendFile(p.absolute('web/index.html'));
    }
  ]);

  // get request that returns json
  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);
    }
  ]);

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

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.

example/example.dart

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

main() {
  var serv = Sevr();
  serv.use(Sevr.static('./web'));

  //We can create controller,middleware classes etc, put them in a list and pass them into the router methods
  serv.get('/file', [
    (ServRequest req, ServResponse res) {
      return res.status(200).sendFile(p.absolute('web/index.html'));
    }
  ]);

  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);
    }
  ]);

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

//  controllerClassOne(req,res)async{
//   res.locals['response'] = req.body;
// }

// controllerReturn(req,res){
//   print('running the second function');
//   return res.status(200).json(res.locals['response']);
// }

Use this package as a library

1. Depend on it

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


dependencies:
  sevr: ^1.0.2+3

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]
39
Health:
Code health derived from static analysis. [more]
98
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
100
Overall:
Weighted score of the above. [more]
69
Learn more about scoring.

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

  • Dart: 2.7.0
  • pana: 0.13.4

Health suggestions

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

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

line 66 col 19: The value of the local variable 'tempOnData' isn't used.

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

line 360 col 14: The value of the local variable 'file' isn't used.

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

Fix lib/src/serv_request_response_wrapper/serv_request_wrapper.dart. (-0.50 points)

Analysis of lib/src/serv_request_response_wrapper/serv_request_wrapper.dart reported 1 hint:

line 5 col 8: Unused import: 'package:sevr/src/http_server/http_server.dart'.

Format lib/src/mime/mime_src/mime_multipart_transformer.dart.

Run dartfmt to format lib/src/mime/mime_src/mime_multipart_transformer.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.2
collection 1.14.12
http_parser 3.1.3
http_server 0.9.8+3
meta 1.1.8
source_span 1.6.0
string_scanner 1.0.5
term_glyph 1.1.0
typed_data 1.1.6
Dev dependencies
test ^1.6.0