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 ();
}