jaguar
1.0.0
Jaguar是一款全棧生產Ready HTTP服務器框架,構建了快速,簡單和直觀的。
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 ();
}路徑段的前綴為:可以匹配任何值,也被捕獲為路徑變量。可以使用Context對象的pathParams成員訪問路徑變量。
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方法可用於輕鬆打字路徑變量。可以使用Context對象的queryParams成員訪問查詢參數。
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方法可用於輕鬆將查詢參數輸入所需類型。
一行是在Request對像上使用Method bodyAsUrlEncodedForm作為Map<String, String>獲得表單所需的全部。
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 ();
}方法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 ();
}