A simple dart http server in Koa2 style

Overview

Dia

Pub Package GitHub Repo stars

A simple dart http server like KoaJS.

This package allows you to create a http / http server in a couple of lines. Dia creates a context from a bunch of request and response and passes it through the middleware.

The main idea of the project is minimalism. The package contains only basic functionality, everything else is implemented in separate packages. This allows you to keep the project code clean and connect only those dependencies that are really needed in it.

Install:

Add to pubspec.yaml in dependencies section this:

    dia: ^0.1.3

Then run pub get

Usage:

A simple usage example:

import 'package:dia/dia.dart';

main() {
  final app = App();
  
  app.use((ctx,next) async {
    ctx.body = 'Hello world!';
  });

  app
      .listen('localhost', 8080)
      .then((info) => print('Server started on http://localhost:8080'));
}

Context contain getters and setters for HttpRequest fields: response, response.headers, response.headers.contentType, response.statusCode, that allow use it easy. Context contain method throwError that allow easy return HTTP errors by statusCode.

Example throwError:

    app.use((ctx,next) async {
      ctx.throwError(401);
    });

You can use custom Context with additional fields and methods. In this case, you need to inherit from the base Context and pass the instance of context creation function to the App constructor.

/// Create custom context class
class CustomContext extends Context{
  String? additionalField;
  CustomContext(HttpRequest request) : super(request);
}

void main() {
  /// Create Dia instance 
  final app = App((request) => CustomContext(request));

  /// Add additionalField value
  app.use((ctx, next) async {
    ctx.additionalField = 'additional value';
    await next();
  });

  /// final middleware to response
  app.use((ctx, next) async {
    ctx.contentType = ContentType.text;
    ctx.body = ctx.additionalField;
  });

  /// Start server listen on localhost:8080
  app
      .listen('localhost', 8080)
      .then((info) => print('Server started on http://localhost:8080'));
}

Your can add handler to all Http errors:

app.use((ctx,next) async {
  await next();
  if(ctx.statusCode!=200){
    //....
  }
});

You can start your server with SSL:

const serverKey = 'cert/key.pem';
const certificateChain = 'cert/chain.pem';

final serverContext = SecurityContext();
serverContext
    .useCertificateChainBytes(await File(certificateChain).readAsBytes());
serverContext.usePrivateKey(serverKey, password: 'password');

app.listen('localhost', 8444, securityContext: serverContext);

For more details, please, see example and test folder.

Use with:

  • dia_router - Package to route request as koa-router.
  • dia_cors - Package for CORS middleware.
  • dia_body - Package with the middleware for parse request body.
  • dia_static - Package to serving static files.

Features and bugs:

I will be glad for any help and feedback! Please file feature requests and bugs at the issue tracker.

You might also like...

A lightweight HTTP server framework for Dart.

A lightweight HTTP server framework for Dart.

arowana A lightweight HTTP server framework for Dart.It is based on the shelf library for handling HTTP requests and implements a high-performance rou

Nov 3, 2022

Dart HTTP server framework for building REST APIs. Includes PostgreSQL ORM and OAuth2 provider.

Dart HTTP server framework for building REST APIs. Includes PostgreSQL ORM and OAuth2 provider.

Aqueduct is a modern Dart HTTP server framework. The framework is composed of libraries for handling and routing HTTP requests, object-relational mapp

Jan 5, 2023

Dart HTTP server framework for building REST APIs. Includes PostgreSQL ORM and OAuth2 provider.

Dart HTTP server framework for building REST APIs. Includes PostgreSQL ORM and OAuth2 provider.

Aqueduct is a modern Dart HTTP server framework. The framework is composed of libraries for handling and routing HTTP requests, object-relational mapp

Jul 7, 2021

Powerful, helpfull, extensible and highly customizable API's that wrap http client to make communication easier with Axelor server with boilerplate code free.

Powerful, helpfull, extensible and highly customizable API's that wrap http client to make communication easier with Axelor server with boilerplate code free.

flutter_axelor_sdk Powerful, helpful, extensible and highly customizable API's that wrap http client to make communication easier with Axelor server w

Dec 25, 2022

Neumorphic style - Example app with Flutter that displays a neumorphic style container

Neumorphic style - Example app with Flutter that displays a neumorphic style container

Flutter Neumorphic Style Example App Example app with Flutter that displays a ne

Mar 24, 2022

Domain-Driven Design + Parse Server For FlutterDomain-Driven Design + Parse Server For Flutter

Domain-Driven Design + Parse Server For FlutterDomain-Driven Design + Parse Server For Flutter

Domain-Driven Design + Parse Server Install Parse SDK on Flutter project and then update .env file PARSE_APPLICATION_ID=YOUR_APP_ID_HERE PARSE_CLIENT_

Nov 18, 2022

💙 A simple Flutter app sample with hooks_riverpod, includes HTTP requests by dio using GitHub APIs.

💙 A simple Flutter app sample with hooks_riverpod, includes HTTP requests by dio using GitHub APIs.

Oct 13, 2022

Simple examples that help you get started with Appwrite + Dart (=❤️) as an Appwrite server-side integration.

Playground for Dart Simple examples that help you get started with Appwrite + Dart (= ❤️ ) This is Appwrite server side integration with Dart. For Flu

Nov 22, 2022

A simple Git LFS server implementation in Dart

A simple Git LFS server implementation in Dart Overview Git LFS client (git-lfs or git lfs command) requires a dedicated LFS-content server (not the G

Oct 31, 2022

A powerful Http client for Dart, which supports Interceptors, FormData, Request Cancellation, File Downloading, Timeout etc.

dio_http A powerful Http client for Dart, which supports Interceptors, Global configuration, FormData, Request Cancellation, File downloading, Timeout

Dec 19, 2021

Future based HTTP client for the Dart and Flutter

Uno Future based HTTP client for the Dart and Flutter. Uno, inspired by Axios, bringing a simple and robust experience to the crossplatform apps in Fl

Dec 16, 2022

Flutter_socks_proxy is a dart package, HTTP/Socks4/Socks5 proxy

flutter_socks_proxy flutter_socks_proxy is a dart package, HTTP/Socks4/Socks5 proxy Usage Use global import 'dart:convert';

Oct 23, 2022

A powerful Http client for Dart, which supports Interceptors, FormData, Request Cancellation, File Downloading, Timeout etc.

A powerful Http client for Dart, which supports Interceptors, FormData, Request Cancellation, File Downloading, Timeout etc.

Language: English | 中文简体 dio A powerful Http client for Dart, which supports Interceptors, Global configuration, FormData, Request Cancellation, File

Jan 3, 2023

A Dart package to handle HTTP services

http_services A package to support the creation of Http services in a Dart application. Features convenient methods to perform HTTP requests disposing

Jul 27, 2021

A composable API for making HTTP requests in Dart.

A composable, Future-based library for making HTTP requests. This package contains a set of high-level functions and classes that make it easy to cons

Dec 26, 2022

A middleware library for Dart's http library.

http_middleware A middleware library for Dart http library. Getting Started http_middleware is a module that lets you build middleware for Dart's http

Oct 23, 2021

A lightweight and customizable http client that allows you to create offline-first dart app easily.

Enjoyable & customizable offline-first REST API client Unruffled is lightweight and customizable http client that allows you to create offline-first e

May 20, 2022

Fetchx - Simplified http requests in Dart

Fetchx Fetchx is a simple, fast, and secure HTTP client for Dart. It leverages extensions to allow using url-like strings to make http requests. Read

Nov 26, 2022

Dart-com-Shelf - Web server básico feito com dart e shelf, configurações de rotas e conexão com mysql.

A server app built using Shelf, configured to enable running with Docker. This sample code handles HTTP GET requests to / and /echo/message Running

Jan 3, 2022
Comments
  • Compilation support?

    Compilation support?

    $>dart --version
    Dart SDK version: 2.12.2 (stable) (Wed Mar 17 10:30:20 2021 +0100) on "linux_x64"
    
    $>dart compile exe bin/example.dart 
    Info: Compiling with sound null safety
    
    error: import of dart:mirrors is not supported in the current Dart runtime
    
    Error: AOT compilation failed
    Generating AOT snapshot failed!
    
    opened by dafanasiev 2
Owner
Andrey Unger
Full Stack Developer
Andrey Unger
A lightweight HTTP server framework for Dart.

arowana A lightweight HTTP server framework for Dart.It is based on the shelf library for handling HTTP requests and implements a high-performance rou

arcticfox 15 Nov 3, 2022
Dart HTTP server framework for building REST APIs. Includes PostgreSQL ORM and OAuth2 provider.

Aqueduct is a modern Dart HTTP server framework. The framework is composed of libraries for handling and routing HTTP requests, object-relational mapp

Abdelrahman Saed 2 Jul 7, 2021
A simple HTTP server that can serve up any directory, built with Dart

A simple HTTP server that can serve up any directory, built with Dart. Inspired by python -m SimpleHTTPServer. Install Use the dart pub global command

Behruz Hurramov 0 Dec 27, 2021
Petit httpd - This is a simple HTTP file server integrated with Let's Encrypt, gzip and CORS

Petit httpd - This is a simple HTTP file server integrated with Let's Encrypt, gzip and CORS

Graciliano Monteiro Passos 4 Nov 24, 2022
App HTTP Client is a wrapper around the HTTP library Dio to make network requests and error handling simpler, more predictable, and less verbose.

App HTTP Client App HTTP Client is a wrapper around the HTTP library Dio to make network requests and error handling simpler, more predictable, and le

Joanna May 44 Nov 1, 2022
Charlatan - A library for configuring and providing fake http responses to your dio HTTP client.

charlatan This package provides the ability to configure and return fake HTTP responses from your Dio HTTP Client. This makes it easy to test the beha

Betterment 14 Nov 28, 2022
http_plus - a drop-in replacement for http with HTTP/2 goodies

http_plus is a drop-in replacement for http with HTTP/2 goodies! Under the hood, it wraps http2 to make it compatible with APIs of http. Additionally, it fallbacks to HTTP/1.1 if H2 is not supported by the server.

Harsh Bhikadia 7 Apr 22, 2022
An easy-to-use flutter http network requests handler with more functionality than http but more simpler than dio.

network_requests An easy-to-use flutter http network requests handler with more functionality than http but more simpler than dio. Platform Supported

Coder_Manuel 3 Dec 15, 2022
Liquidart is a Dart HTTP server framework for building REST APIs. Includes PostgreSQL ORM and OAuth2 provider. Revival of the abandoned AQUEDUCT project.

Liquidart is a modern Dart HTTP server framework. The framework is composed of libraries for handling and routing HTTP requests, object-relational map

Aldrin's Art Factory 35 Dec 27, 2022
server side dart micro-framework to handle incoming http requests

Queen Palace ???? Introduction server side dart micro-framework to handle incoming http requests

Ahmed Masoud 32 Aug 30, 2022