jaguar
Jaguar는 속도, 간단함 및 확장성을 위해 설계된 프로덕션 준비 완료 HTTP 서버 프레임워크입니다
Jaguar는 속도, 단순성 및 확장성을 위해 구축된 서버 프레임워크입니다. ORM, 세션, 인증 및 권한 부여, OAuth
아래는 영문 원문 스냅샷입니다. 최신 내용은 GitHub에서 확인하세요.
Jaguar is a full-stack production ready HTTP server framework built to be fast, simple and intuitive.
Jaguar class provides methods get, put, post,
delete and options to quickly add route handlers for specific HTTP methods.
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();
}
Path segments prefixed with : can match any value and are also captured as path variables. Path variables can be accessed
using pathParams member of 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/:index', (ctx) { // The magic!
final int index = ctx.pathParams.getInt('index', 1); // The magic!
return quotes[index + 1];
});
await server.serve();
}
getInt, getDouble, getNum and getBool methods can be used to easily typecast path variables.Query parameters can be accessed using queryParams member of 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 and getBool methods can be used to easily typecast query parameters into desired type.
A single line is all it takes to obtain a form as a Map<String, String> using method bodyAsUrlEncodedForm
on Request object.
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();
}
The method staticFiles adds static files to Jaguar server. The first argument determines
the request Uri that much be matched and the second argument determines the directory from which the target files are fetched.
main() async {
final server = Jaguar();
server.staticFiles('/static/*', 'static'); // The magic!
await server.serve();
}
Decoding JSON requests can't be simpler than using one of the built-in bodyAsJson,
bodyAsJsonMap or bodyAsJsonList methods on
Request object.
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();
}