Jaguar est un framework de serveur HTTP de production complet de production conçu pour être rapide, simple et intuitif.
La classe Jaguar fournit des méthodes get , put , post , delete et options pour ajouter rapidement des gestionnaires de routes pour des méthodes HTTP spécifiques.
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 ();
} Les segments de chemin préfixés avec : peuvent correspondre à n'importe quelle valeur et sont également capturés sous forme de variables de chemin. Les variables de chemin sont accessibles à l'aide du membre pathParams de l'objet 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 et getBool peuvent être utilisées pour facilement les variables de trajet en dactylographie. Les paramètres de requête sont accessibles à l'aide du membre queryParams de l'objet 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 ();
} Les méthodes getInt , getDouble , getNum et getBool peuvent être utilisées pour être facilement en désaccord sur les paramètres de requête dans le type souhaité.
Une seule ligne est tout ce qu'il faut pour obtenir un formulaire en tant que Map<String, String> en utilisant la méthode bodyAsUrlEncodedForm sur l'objet 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 ();
} La méthode staticFiles ajoute des fichiers statiques au serveur Jaguar . Le premier argument détermine l'URI de la demande que beaucoup soit correspondant et le deuxième argument détermine le répertoire à partir duquel les fichiers cibles sont récupérés.
main () async {
final server = Jaguar ();
server. staticFiles ( '/static/*' , 'static' ); // The magic!
await server. serve ();
} Le décodage des demandes JSON ne peut pas être plus simple que d'utiliser l'une des méthodes bodyAsJson , bodyAsJsonMap ou bodyAsJsonList sur 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 ();
}