จากัวร์เป็นเฟรมเวิร์ก HTTP Server ที่สร้างขึ้นอย่างเต็มรูปแบบที่สร้างขึ้นเพื่อให้เร็วง่ายและใช้งานง่าย
Jaguar Class มีวิธีการ 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 สามารถใช้วิธีการพิมพ์ตัวแปรเส้นทาง Typecast ได้อย่างง่ายดาย พารามิเตอร์การสืบค้นสามารถเข้าถึงได้โดยใช้สมาชิก 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 สามารถใช้วิธีการค้นหา Typecast Query ได้อย่างง่ายดายเป็นประเภทที่ต้องการ
บรรทัดเดียวคือทั้งหมดที่ใช้ในการรับแบบฟอร์มเป็น 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 Server อาร์กิวเมนต์แรกกำหนด 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 ();
}