Skip to content

Instantly share code, notes, and snippets.

@graphicbeacon
Created May 16, 2018 21:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save graphicbeacon/d130b47478288c7a5f476c84e5a6b030 to your computer and use it in GitHub Desktop.
Save graphicbeacon/d130b47478288c7a5f476c84e5a6b030 to your computer and use it in GitHub Desktop.
Sample code for "Building RESTful Web APIs with Dart, Aqueduct and PostgreSQL (Part 2)" post on Medium (2)
router.route('/books[/:index]').listen((Request incomingRequest) async {
String reqMethod = incomingRequest.innerRequest.method;
String index = incomingRequest.path.variables["index"];
if (reqMethod == 'GET') {
if(index != null) {
return new Response.ok('Showing book by index: $index');
}
return new Response.ok('Showing all books.');
} else if (reqMethod == 'POST') {
return new Response.ok('Added a book.');
} else if (reqMethod == 'PUT') {
return new Response.ok('Added a book.');
} else if (reqMethod == 'DELETE') {
return new Response.ok('Added a book.');
}
// If all else fails
return new Response(405, null, 'Not sure what you\'re asking here');
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment