A simple dart http server in Koa2 style

Dia

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.

GitHub

https://github.com/unger1984/dia