GithubHelp home page GithubHelp logo

elias8 / darpress Goto Github PK

View Code? Open in Web Editor NEW
6.0 1.0 0.0 14 KB

Easy to use, lightweight, annotation free, and no source generation server framework for dart inspired by expressjs.

License: MIT License

Dart 100.00%
dart server-framework darpress

darpress's Introduction

Darpress

WARNING: This is highly experimental.

Darpress is easy to use, lightweight, annotation free, and no source generation server framework for dart inspired by expressjs.

Usage

A simple usage example:

import 'package:darpress/darpress.dart';

void main() async{
  final app = Darpress();
  app.post('/todos', (request, response, next) async{
  //TODO: write your logic here.
  await response.send('some response');
  });
  
  app.post('/todos', (request, response, next) async{
  //TODO: write your logic here
  await response.send({'key': 'value'});
  });
    
  final server = await app.listen(); // default port number is 3000
  // you can define the port number if you want.
  // final server = await app.listen(port: 5000);
}

As the routes for your app increases, you may want to use a CustomRouter. It will make your code more organized and easily readable.

import 'package:darpress/darpress.dart';

class UserRouter implements CustomRouter {
  @override
  void route(Router router) {
    router.post('/users', _postUser);
    router.get('/users', _getUsers);
    // OR you can use the builder.
    // router.route('/users').post(_postUser).get(_getUsers);
  }

  Future<void> _getUsers(Request request, Response response, next) async {
    //TODO: write your logic here.
    await response.send('some user data');
  }

  Future<void> _postUser(Request request, Response response, next) async {
    //TODO: write your logic here.
    await response.send('saved user data');
  }
 }

Now you can use the UserRouter in your app.

void main() async{
   final app = Darpress();
   app.route(UserRouter());
   final server = await app.listen();
   // OR you can use the shortcut like this.
   // Darpress().route(UserRouter()).listen();
 }

You can add multiple middlewares on a route.

void main() async {
  final app = Darpress();
  app.get('/colors', (request, response, next) async {
    print('Running the first middleware..');
    await next();
  }, [
    (request, response, next) async {
      print('Running the second middleware..');
      await next();
    },
    (request, response, next) async {
      print('Running the third middleware..');
      await next();
    },
    (request, response, next) async {
      print('Running the last middleware..');
      response.send('Middleware example.');
    }
  ]);
  
  app.listen();
}

Examples

  • Examples - an example of how to use a custom router and middleares.

Features and bugs

Please file feature requests and bugs at the issue tracker.

Maintainers

darpress's People

Contributors

elias8 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.