jaguar
1.0.0
Jaguar는 빠르고 단순하며 직관적으로 구축 된 풀 스택 프로덕션 준비 HTTP 서버 프레임 워크입니다.
Jaguar Class는 특정 HTTP 방법에 대한 경로 처리기를 신속하게 추가 할 수있는 메소드 get , put , post , delete 및 options 제공합니다.
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 Object의 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 메소드를 사용하여 경로 변수를 쉽게 타이핑하는 데 사용할 수 있습니다. queryParams 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) {
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 ();
} Method staticFiles 정적 파일을 Jaguar 서버에 추가합니다. 첫 번째 인수는 일치하는 요청 URI를 결정하고 두 번째 인수는 대상 파일이 가져 오는 디렉토리를 결정합니다.
main () async {
final server = Jaguar ();
server. staticFiles ( '/static/*' , 'static' ); // The magic!
await server. serve ();
} Decoding JSON 요청은 Request 객체에서 내장 된 bodyAsJson , bodyAsJsonMap 또는 bodyAsJsonList 메소드 중 하나를 사용하는 것보다 간단 할 수 없습니다.
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 ();
}