Jaguar هو إطار عمل HTTP Server جاهز للإنتاج الكامل ليكون سريعًا وبسيطًا وبديهيًا.
يوفر فئة Jaguar الأساليب get الأساليب ، put ، post ، delete options لإضافة معالجات الطريق بسرعة لطرق HTTP محددة.
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 ();
} مقاطع المسار المسبقة مع : يمكن أن تتطابق مع أي قيمة ويتم التقاطها أيضًا كمتغيرات مسار. يمكن الوصول إلى متغيرات المسار باستخدام عضو pathParams في كائن Context .
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 ();
}getInt و getDouble و getNum و getBool لتنشيط متغيرات المسار بسهولة. يمكن الوصول إلى معلمات الاستعلام باستخدام عضو queryParams في كائن Context .
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 و getBool لتكوين معلمات الاستعلام بسهولة في النوع المطلوب.
السطر الواحد هو كل ما يتطلبه الأمر للحصول على نموذج Map<String, String> باستخدام method bodyAsUrlEncodedForm على كائن Request .
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 ();
} تضيف Method staticFiles ملفات ثابتة إلى خادم Jaguar . تحدد الوسيطة الأولى طلب URI الذي يتم مطابقته كثيرًا ويحدد الوسيطة الثانية الدليل الذي يتم من خلاله جلب الملفات المستهدفة.
main () async {
final server = Jaguar ();
server. staticFiles ( '/static/*' , 'static' ); // The magic!
await server. serve ();
} لا يمكن أن تكون طلبات فك تشفير JSON أبسط من استخدام أساليب bodyAsJson المدمجة أو bodyAsJsonMap أو bodyAsJsonList عند كائن Request .
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 ();
} 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 ();
}