dia 0.1.3
dia: ^0.1.3 copied to clipboard

A simple dart http server in Koa2 style. Use with dia_router, dia_static, dia_body and other packages

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.

11
likes
120
pub points
45%
popularity

Publisher

awcoding.com

A simple dart http server in Koa2 style. Use with dia_router, dia_static, dia_body and other packages

Repository (GitHub)
View/report issues

Documentation

API reference

License

MIT (LICENSE)

More

Packages that depend on dia