搜索插件、代码仓与 APP 分类

插件详情v3.1.4

jaguar

Jaguar 是一个为速度、简单性和可扩展性而构建的生产就绪 HTTP 服务器框架

AndroidiOSmacOSWindowsLinux
近 30 天下载
6630
喜欢数
63
Pub 分数
120

安装插件

flutter pub add jaguar

插件介绍

基于插件公开信息、社区数据与项目使用情况整理的选型参考。

Jaguar 是一个高性能、生产就绪的 Dart HTTP 服务器框架,旨在实现速度、简洁性和可扩展性。它专注于开发者体验,能够快速开发可扩展的后端服务和 Web API。Jaguar 支持异步编程,并提供模块化架构,非常适合小型项目和大型应用。它可在 Android、iOS、macOS、Windows 和 Linux 等多个平台上无缝运行。

适合场景

  • 构建 RESTful API
  • 创建微服务
  • 开发实时网络服务器
  • 为移动应用实现后端逻辑
  • 运行服务器端 Dart 应用程序

主要特点

  • 高性能 HTTP 服务器
  • 模块化和可扩展的设计
  • 对 async/await 的内置支持
  • 轻松路由与中间件集成
  • 跨平台兼容性

适合对象

  • Dart 开发人员构建后端服务
  • 需要快速可靠HTTP服务器的团队
  • 需要轻量级和可扩展 API 的项目
  • 使用 Dart 的移动应用后端
  • 全栈 Dart 应用

使用建议

  • 与成熟框架相比,生态系统有限
  • 需要理解 Dart 的异步模型
  • 比 Express.js 或 Spring Boot 等替代方案更小的社区

原始 README

以下为关联源码仓库的 README 原文快照,最新内容请访问源码仓库。

Pub Build Status Gitter

Jaguar

Jaguar is a full-stack production ready HTTP server framework built to be fast, simple and intuitive.

Getting started

Familiar way to write routes

Jaguar class provides methods get, put, post, delete and options to quickly add route handlers for specific HTTP methods.

main() async {
  final server = Jaguar();  // Serves the API at localhost:8080 by default
  // Add a route handler for 'GET' method at path '/hello'
  server.get('/hello', (Context ctx) => 'Hello world!');
  await server.serve();
}

Powerful route matching

Easily define and access Path parameters

Path segments prefixed with : can match any value and are also captured as path variables. Path variables can be accessed using pathParams member of Context object.

main(List<String> args) async {
  final quotes = <String>[
    'But man is not made for defeat. A man can be destroyed but not defeated.',
    'When you reach the end of your rope, tie a knot in it and hang on.',
    'Learning never exhausts the mind.',
  ];

  final server = Jaguar();
  server.get('/api/quote/:index', (ctx) { // The magic!
    final int index = ctx.pathParams.getInt('index', 1);  // The magic!
    return quotes[index + 1];
  });
  await server.serve();
}
  • A path can have multiple path variables.
  • A path variable can appear at any position in the path.
  • A path variable can be matched against a Regular expression.
  • getInt, getDouble, getNum and getBool methods can be used to easily typecast path variables.
  • Using * as the final path segment captures/matches all following segments.

Easily access Query parameters

Query parameters can be accessed using queryParams member of Context object.

main(List<String> args) async {
  final quotes = <String>[
    'But man is not made for defeat. A man can be destroyed but not defeated.',
    'When you reach the end of your rope, tie a knot in it and hang on.',
    'Learning never exhausts the mind.',
  ];

  final server = Jaguar();
  server.get('/api/quote', (ctx) {
    final int index = ctx.queryParams.getInt('index', 1); // The magic!
    return quotes[index + 1];
  });
  await server.serve();
}

getInt, getDouble, getNum and getBool methods can be used to easily typecast query parameters into desired type.

One liner to access Forms

A single line is all it takes to obtain a form as a Map<String, String> using method bodyAsUrlEncodedForm on Request object.

main(List<String> arguments) async {
  final server = Jaguar(port: 8005);

  server.postJson('/api/add', (ctx) async {
      final Map<String, String> map = await ctx.req.bodyAsUrlEncodedForm(); // The magic!
      contacts.add(Contact.create(map));
      return contacts.map((ct) => ct.toMap).toList();
    });


  await server.serve();
}

One liner to serve static files

The method staticFiles adds static files to Jaguar server. The first argument determines the request Uri that much be matched and the second argument determines the directory from which the target files are fetched.

main() async {
  final server = Jaguar();
  server.staticFiles('/static/*', 'static'); // The magic!
  await server.serve();
}

JSON serialization with little effort

Decoding JSON requests can't be simpler than using one of the built-in bodyAsJson, bodyAsJsonMap or bodyAsJsonList methods on Request object.

Future<void> main(List<String> args) async {
  final server = Jaguar();
  server.postJson('/api/book', (Context ctx) async {
    // Decode request body as JSON Map
    final Map<String, dynamic> json = await ctx.req.bodyAsJsonMap();
    Book book = Book.fromMap(json);
    return book; // Automatically encodes Book to JSON
  });

  await server.serve();
}

Out-of-the-box Sessions support

main() async {
  final server = Jaguar();
  server.get('/api/add/:item', (ctx) async {
    final Session session = await ctx.req.session;
    final String newItem = ctx.pathParams.item;

    final List<String> items = (session['items'] ?? '').split(',');

    // Add item to shopping cart stored on session
    if (!items.contains(newItem)) {
      items.add(newItem);
      session['items'] = items.join(',');
    }

    return Response.redirect('/');
  });
  server.get('/api/remove/:item', (ctx) async {
    final Session session = await ctx.req.session;
    final String newItem = ctx.pathParams.item;

    final List<String> items = (session['items'] ?? '').split(',');

    // Remove item from shopping cart stored on session
    if (items.contains(newItem)) {
      items.remove(newItem);
      session['items'] = items.join(',');
    }

    return Response.redirect('/');
  });
  await server.serve();
}

所属分类

依赖

依赖清单 11 项

源码仓库

使用此插件的项目