从零建立一个 Dart 服务端项目

前几天看了咸鱼的一篇文章 Flutter & Dart三端一体化开发 , 相信有一部分人想使用 Dart 尝试编写一些服务端的代码.html

DartVM 的性能已经和 JVM 很是接近, 再加上和 Nodejs 同样的异步io的处理方式, 能够达到和 Nodejs 同级别的并发性能, 而且还有更好地运算性能, 彻底能够替代大部分 Nodejs 的使用场景.mongodb

这篇文章将会带你们从零建立一个 Dart 服务端项目, 本文会逐步覆盖一下知识点:数据库

  • 从一个空文件夹开始, 安装依赖库, 编写可执行的项目
  • 编写 API, 而且读取 GET 请求的参数 和 POST 请求的 body
  • mongodb 或其余数据库的链接
  • 编写请求前置中间键, 而且扩展 db 对象至请求的上下文
  • 进行 AOT 编译和部署

安装 Dart

MacOS:express

$ brew tap dart-lang/dart
 $ brew install dart
复制代码

Windows(使用 chocolatey 安装):跨域

c:\ choco install dart-sdk
复制代码

或者根据 Dart官方文档安装bash

建立一个 Dart 项目

建立一个文件夹, 而且建立一个 pubspec.yaml 文件并发

$ mkdir your_project && cd your_project
$ touch pubspec.yaml
复制代码

pubspec.yaml 文件内容:app

name: your_project
version: 0.0.1
environment:
 sdk: '>=2.3.0 <3.0.0'

dependencies:
 serral: any
复制代码

安装依赖:koa

$ pub get
复制代码

这里咱们添加了一个依赖 serral 做为 express 或 koa 的替代品进行服务端开发, 它的源码只有 200 行, 而且约定了一套很是简单的扩展方式, Serral API 文档.异步

编写你的第一个 Dart 服务

$ mkdir lib
$ touch lib/main.dart
复制代码

编辑 lib/main.dart:

import 'package:serral/main.dart';

void main() {
  final app = Serral();

  // 默许跨域
  app.before(app.addCorsHeaders);

  // 添加前置中间键
  app.before((SerralCtx ctx) {
    print(ctx.request.uri.toString());
    ctx.context['dog'] = 100;
  });

  // 添加后置中间键
  app.after((SerralCtx ctx) {
    print('end');
  });

  // 捕获某个路径的请求
  app.GET('/', getHome);
  app.POST('/dog', postDog);

  app.serve(port: 5100);
}

// 实现该 GET 路由
void getHome(SerralCtx ctx) async {
  // 读取 ctx.context, 检查前置中间键是否生效
  print(ctx.context['dog']);
  // 查看请求路径参数
  print(ctx.params);
  ctx.send(200, 'hello: ${ctx.context['dog']}');
}

// 实现该 POST 路由
void postDog(SerralCtx ctx) async {
  // 查看 post 请求的 body
  print(ctx.body);
  // 模拟异步, 检查后置中间键是否生效
  await Future.delayed(Duration(milliseconds: 300));
  ctx.send(200, 'order');
}
复制代码

启动服务

$ dart lib/main.dart
复制代码

好了, 服务已经启动:

serral runing: http://127.0.0.1:5100
复制代码

如何使用 Mongodb 或其余数据驱动

安装 mongo_dart:

dev_dependencies:
 mongo_dart: any
复制代码

方案 1, 利用 context 存储驱动:

编写代码

import 'package:mongo_dart/mongo_dart.dart';

import 'package:serral/main.dart';

void main() async {
  Db db = new Db("mongodb://127.0.0.1:27017/test");
  await db.open();

  final app = Serral();

  app.before((SerralCtx ctx) {
    // add mongodb in context
    ctx.context['db'] = db;
  });

  app.GET('/', getHome);

  app.serve(port: 5100);
}

void getHome(SerralCtx ctx) async {
  // 在请求响应时获取 db对象
  Db db = ctx.context['db'];
  print(db);
  ctx.send(200, 'hello: ${ctx.context['dog']}');
}
复制代码

方案 2, 使用 mixin 扩展 SerralCtx:

import 'package:mongo_dart/mongo_dart.dart';

import 'package:serral/main.dart';

// mixin 扩展 SerralCtx 来添加各类所需的对象
class MongoCtx with SerralCtx {
  Db db;
}

void main() async {
  Db db = new Db("mongodb://127.0.0.1:27017/test");
  await db.open();

  // 使用 MongoCtx 替换 SerralCtx 做为上下文
  final app = Serral(()=> MongoCtx());

  app.before((MongoCtx ctx) {
    // 在请求前置的中间键存储 db 对象的引用
    ctx.db = db;
  });

  app.GET('/', getHome);

  app.serve(port: 5100);
}

void getHome(MongoCtx ctx) async {
  // 在请求响应中使用 db 对象
  print(ctx.db);
  ctx.send(200, 'hello: ${ctx.context['dog']}');
}
复制代码

好的, 经过以上的例子咱们能够很轻松的给服务添加前置或后置的中间键, 或者在扩展 context 对象的内容, 方便在请求响应时进行使用.

AOT编译及部署

接下来咱们要 DartVM 的性能, 咱们将 source-code 进行 AOT 编译, AOT编译后相对于 source-code 能够提高1~2个数量级的性能:

AOT编译:

dart2aot lib/main.dart lib/main.aot
复制代码

使用 dartaotruntime 启动生产版本:

dartaotruntime lib/main.aot
复制代码

序也能够写在最后, 不是吗?

经过简单的一点点代码, 咱们已经建立了一个 Dart API 服务, 而且进行了 AOT 编译, 让其更合适在生产环境下运行.

但愿刚开始投资 Dart 的童鞋会有些许收获, 谢谢阅读.

相关文章
相关标签/搜索