Aqueduct에서 서버 측 Dart를 사용해보십시오.

5219 단어 Dart

준비



Dart 설치


$ brew tap dart-lang/dart
$ brew install dart

참고 htps : //이다 rt. 에서 v / 게 t-rt

Aqueduct 설치


$ pub global activate aqueduct
$ export PATH="$PATH":"$HOME/.pub-cache/bin" # shellファイルに追加

프로젝트 만들기


$ aqueduct create my_project

튜토리얼



자습서 프로젝트 만들기


$ aqueduct create heroes

영웅 목록의 끝점



lib/controller/heroes_controller.dart 만들기

import 'package:aqueduct/aqueduct.dart';
import 'package:heroes/heroes.dart';

class HeroesController extends Controller {
  final _heroes = [
    {'id': 11, 'name': 'Mr. Nice'},
    {'id': 12, 'name': 'Narco'},
    {'id': 13, 'name': 'Bombasto'},
    {'id': 14, 'name': 'Celeritas'},
    {'id': 15, 'name': 'Magneta'},    
  ];

  @override
  Future<RequestOrResponse> handle(Request request) async {
    return Response.ok(_heroes);
  }
}

lib/channel.dart 수정


import 'heroes.dart';
+ import 'controller/heroes_controller.dart';

/// This type initializes an application.
///
/// Override methods in this class to set up routes and initialize services like
/// database connections. See http://aqueduct.io/docs/http/channel/.
class HeroesChannel extends ApplicationChannel {
  /// Initialize services in this method.
  ///
  /// Implement this method to initialize services, read values from [options]
  /// and any other initialization required before constructing [entryPoint].
  ///
  /// This method is invoked prior to [entryPoint] being accessed.
  @override
  Future prepare() async {
    logger.onRecord.listen((rec) => print("$rec ${rec.error ?? ""} ${rec.stackTrace ?? ""}"));
  }

  /// Construct the request channel.
  ///
  /// Return an instance of some [Controller] that will be the initial receiver
  /// of all [Request]s.
  ///
  /// This method is invoked after [prepare].
  @override
  Controller get entryPoint {
    final router = Router();
+
+    router
+      .route('/heroes')
+      .link(() => HeroesController());

    // Prefer to use `link` instead of `linkFunction`.
    // See: https://aqueduct.io/docs/http/request_controller/
    router
      .route("/example")
      .linkFunction((request) async {
        return Response.ok({"key": "value"});
      });

    return router;
  }
}

서버 시작
$ aqueduct serve

http://localhost:8888/heroes 에서 결과를 볼 수 있습니다.

좋은 웹페이지 즐겨찾기