简单的 Dart TODO API

基于 shelf 包的非常简单的 Dart TODO API。

该项目旨在演示如何使用 Dart 创建简单的 REST API。

下载依赖项

如前所述,该项目依赖于 shelf 包。要下载包并使其可用于项目,请运行

dart pub get

编译并运行

假设已安装 Dart SDK(可在此处找到说明),可以使用以下命令从项目根目录编译 SDK:

dart compile exe bin/todo_dart

这将创建一个可独立运行的可执行文件,通过从项目根目录运行以下命令来执行:

./bin/todo_dart.exe

运行调试构建

要运行调试构建,请从项目根目录运行以下命令:

dart run bin/todo_dart.dart

测试 API

如果服务器正在运行,它将开始在端口 8080 上的 localhost 上监听。要使用 curl 从 API 获取 TODO 项目列表,请运行:

curl https://:8080/todos

要使用 curl 将项目添加到 TODO 列表,请运行:

curl -X 'POST' \
  'https://:8080/todo' \
  -H 'accept: application/json' \
  -H 'Content-Type: application/json' \
  -d '{
  "id": 2,
  "is_checked": true,
  "value": "Put ice-cream in the freezer"
}'

GitHub

查看 Github