Jaguar 是一個高效率、生產就緒的 Dart HTTP 伺服器框架,專注於速度、簡潔性和可擴展性。以開發者體驗為導向,能快速開發可擴展的後端服務與 Web API。Jaguar 支援非同步程式設計,並具備模組化架構,非常適合小型專案與大型應用。它可在 Android、iOS、macOS、Windows 與 Linux 等多個平台無縫運行。
適用情境
建構 RESTful API
建立微服務
開發即時網路伺服器
為移動應用實現後端邏輯
執行伺服器端 Dart 應用程式
主要特色
高效能 HTTP 伺服器
模組化且可擴充的設計
對 async/await 的內建支援
輕鬆路由與中介軟體整合
跨平台相容性
適合對象
Dart 開發人員建構後端服務
需要快速可靠HTTP伺服器的團隊
需要輕量級和可擴展 API 的項目
使用 Dart 的移動應用後端
全-stack Dart 應用
使用建議
與成熟框架相比,生態系統有限
需要理解 Dart 的非同步模型
比 Express.js 或 Spring Boot 等替代方案更小的社群
原始 README
以下為關聯原始碼儲存庫的 README 原文快照,最新內容請造訪原始碼儲存庫。
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!finalint 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) {
finalint 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.
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();
}