Backend with Dart? Yes, you can!

odinachi

Odinachi David

Posted on February 9, 2022

Backend with Dart? Yes, you can!

Have you ever wondered if it was possible to write a backend in dart? Yeah, you probably have because I also had some curiosity after learning flutter, the idea of having my backend and mobile app in one language was quite a desire I so long for and that led me to making research, in the course of my research I found the shelf server which is a middleware web server which encourages composition and easy reuse. Shelf Router on the other hand does the heavy lifting of route management and response/request mapping.

Below is an example of a simple shelf server:

https://pub.dev/packages/shelf
 import 'package:shelf/shelf.dart';
 import 'package:shelf/shelf_io.dart' as shelf_io;
 void main() async {
  var handler =
  const Pipeline().addMiddleware(logRequests()).addHandler(_echoRequest);
 var server = await shelf_io.serve(handler, 'localhost', 8080);
 // Enable content compression
  server.autoCompress = true;
 print('Serving at http://${server.address.host}:${server.port}');
 }
 Response _echoRequest(Request request) =>
  Response.ok('Request for "${request.url}"');
Enter fullscreen mode Exit fullscreen mode

Let's discuss what you should expect:

  1. Host: this is the location at which your server is at the moment. 

  2. Port: this is the part of the location which belongs to you. 

  3. Pipeline: this is a channel that intercepts every request that passes through the specified paths. 

  4. Middleware: these are a bunch of commands that are stacked on the pipeline to handle requests. 

  5. Handlers: these are future responses, which makes it easier to handle responses in whichever way you wish. 

  6. Request: these are calls being made by the user to your server and can contain different types of parameters. 

  7. Response: this is what's expected of your server after each request is made. 

  8. Path: these are authorized passages from which users can request make a request. 

let's take a look at a simple shelf server sample: 
https://odinachi.github.io/shelf_server/#/
Below is the code:
https://github.com/Odinachi/shelf_server

for Video Tutorials, I'll recommend: https://www.youtube.com/c/CreativeBracket

Notable mentions:

  1. Aqueduct: this was supposed to be a modern Dart HTTP server framework, but somehow got discontinued. Read more about Aqueduct https://aqueduct.io/

  2. Angel: this is quite promising and looks good, read more about Angel-Dart https://angel-dart.dev/

  3. Alfred: this is an express.js like a dart server and can be very helpful if you're coming from a JavaScript background, read more about Alfred https://medium.com/@iapicca/alfred-an-express-like-server-framework-written-in-dart-1661e8963db9

  4. Conduit: this will be my next adventure as it's feature-packed and, with a built-in orm mapper. Read more about Conduit https://www.theconduit.dev/

💖 💪 🙅 🚩
odinachi
Odinachi David

Posted on February 9, 2022

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related