A library for building REST APIs easily with Dart

Related tags

Templates dart-sevr
Overview

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! ๐Ÿ˜€

You might also like...

A library to easily handle sequential queueing of futures in dart.

queue Easily queue futures and await their values. This library allows you to send a future to central queue. The queue will execute the futures in th

Jan 4, 2023

Public interface definitions of Google APIs for Dart and gRPC

Public interface definitions of Google APIs for Dart and gRPC. Getting started I

Feb 23, 2022

Get google api credentials - A Dart CLI app to obtain credentials for accessing Google APIs

get_google_api_credentials A Dart CLI app to obtain credentials for accessing Go

Jan 28, 2022

Flying Fish is full-stack Dart framework - a semi-opinionated framework for building applications exclusively using Dart and Flutter

Flying Fish is full-stack Dart framework - a semi-opinionated framework for building applications exclusively using Dart and Flutter.

Dec 27, 2022

A dart package to interact with the WooCommerce REST API.

A dart package to interact with the WooCommerce REST API.

WooCommerce SDK for Dart A dart package to interact with the WooCommerce API (now with null-safety). It uses OAuth1.0a behind the scenes to generate t

Dec 28, 2022

Developed using Dart & Flutter & Rest Api & Dio & Bloc & SharedPreferenes.

Developed using Dart & Flutter & Rest Api & Dio & Bloc & SharedPreferenes.

This is an ecommerce app which contain Many features like : Sign in , Sign up , Verify Email , log out. Fetch Products Data Search for any product Add

Nov 3, 2022

A UI library for easily adding audio waveforms to your apps, with several customization options

A UI library for easily adding audio waveforms to your apps, with several customization options

A UI library for easily adding audio waveforms to your apps, with several custom

Dec 11, 2022

๐Ÿš€ Simple library to download files easily.

Dart DL Simple library to download files easily. Links GitHub Pub.dev Documentation Features Uses native dart:io to get data. Ability to parse differe

Feb 11, 2022

Toast Library for Flutter, Easily create toast messages in single line of code

Toast Library for Flutter, Easily create toast messages in single line of code

Create Toast messages on a Flutter Event. fluttertoast: ^8.0.8 Toast Library for Flutter, Easily create Personalised toast messages in single line of

Feb 14, 2022
Comments
  • Because every version of sevr depends on body_parser ^1.1.1 which depends on http_parser ^3.1.1, every version of sevr requires http_parser ^3.1.1.

    Because every version of sevr depends on body_parser ^1.1.1 which depends on http_parser ^3.1.1, every version of sevr requires http_parser ^3.1.1.

    when i try to add this package sevr: ^1.0.4 to my pubspec.yaml and run flutter pub get if shows this error:

    Because every version of sevr depends on body_parser ^1.1.1 which depends on http_parser ^3.1.1, every version of sevr requires http_parser ^3.1.1. And because http >=0.13.0 depends on http_parser ^4.0.0, sevr is incompatible with http >=0.13.0. Because basic_utils >=3.3.1 depends on http ^0.13.3 and basic_utils >=3.0.0-nullsafety.0 <3.3.1 depends on http ^0.13.0, basic_utils >=3.0.0-nullsafety.0 requires http ^0.13.0. Thus, sevr is incompatible with basic_utils >=3.0.0-nullsafety.0. And because mongo_dart 0.7.4 depends on basic_utils ^3.0.0 and no versions of mongo_dart match >0.7.4 <0.8.0, sevr is incompatible with mongo_dart ^0.7.4. So, because e_commerce depends on both mongo_dart ^0.7.4 and sevr ^1.0.4, version solving failed.

    Here is my pubspec.yaml:

    #The following adds the Cupertino Icons font to your application. #Use with the CupertinoIcons class for iOS style icons. cupertino_icons: ^1.0.2 gradient_textfield: ^0.0.1 flutter_staggered_grid_view: ^0.6.1 mongo_dart: ^0.7.4 sevr: ^1.0.4 flutter_number_picker: ^1.0.4 provider: ^6.0.2 analyzer: ^3.3.1

    opened by YawarOsman 0
Scouter is a package which was made following the goal to provide a NestJS-like experience to Dart Developers that want to develop Rest APIS

Scouter is a package which was made following the goal to provide a NestJS-like experience to Dart Developers that want to develop Rest APIS Features

Vinicius Amรฉlio 3 Sep 12, 2022
A mobile client for the public apis repository, 1400+ free apis to use able to be navigated through your phone :)

Public APIs mobile app Your assistant app that will help you discover and pick the next API for your next development project What it contains, you sa

Gwhyyy 4 Dec 25, 2022
A most easily usable cookie management library in Dart. With SweetCookieJar, you can easily manage cookie on your application.

A most easily usable cookie management library in Dart! 1. About 1.1. Introduction 1.1.1. Install Library 1.1.2. Import It 1.1.3. Use SweetCookieJar 1

Kato Shinya 9 Oct 27, 2022
A most easily usable cache management library in Dart. With CacheStorage, you can easily manage cache on your application.

A most easily usable cache management library in Dart! 1. About 1.1. Introduction 1.1.1. Install Library 1.1.2. Import It 1.1.3. Use CacheStorage 1.2.

Kato Shinya 1 Dec 13, 2021
A most easily usable RESAS API wrapper in Dart. With this library, you can easily integrate your application with the RESAS API.

A most easily usable RESAS API wrapper library in Dart! 1. About 1.1. What Is RESAS? 1.2. Introduction 1.2.1. Install Library 1.2.2. Import It 1.2.3.

Kato Shinya 2 Apr 7, 2022
A most easily usable Duolingo API wrapper in Dart. Duolingo4D is an open-sourced Dart library.

A most easily usable Duolingo API wrapper in Dart! 1. About Duolingo4D Duolingo4D is an open-sourced Dart library. With Duolingo4D, you can easily int

Kato Shinya 18 Oct 17, 2022
AuthorizationHeader is an open-sourced Dart library. With AuthorizationHeader, you can easily manage authorization header on your application.

A most easily usable authorization header management library in Dart! 1. About 1.1. Supported 1.1.1. Authorization Header 1.1.2. Authorization Type 1.

Kato Shinya 3 Dec 24, 2021
A most easily usable JSON wrapper library in Dart

A most easily usable JSON response wrapper library in Dart! 1. About 1.1. Introd

Kato Shinya 2 Jan 4, 2022
A most easily usable improvement rate calculator library in Dart.

A most easily usable improvement rate calculator library in Dart. With ImprovementRate, you can easily calculate improvement rate on your application.

Kato Shinya 1 Dec 27, 2021