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}"');
Let's discuss what you should expect:
Host: this is the location at which your server is at the moment.
Port: this is the part of the location which belongs to you.
Pipeline: this is a channel that intercepts every request that passes through the specified paths.
Middleware: these are a bunch of commands that are stacked on the pipeline to handle requests.
Handlers: these are future responses, which makes it easier to handle responses in whichever way you wish.
Request: these are calls being made by the user to your server and can contain different types of parameters.
Response: this is what's expected of your server after each request is made.
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:
Aqueduct: this was supposed to be a modern Dart HTTP server framework, but somehow got discontinued. Read more about Aqueduct https://aqueduct.io/
Angel: this is quite promising and looks good, read more about Angel-Dart https://angel-dart.dev/
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
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/
Top comments (4)
Does this compile to another language or does it run on the dart machine? I don't know anything about Dart, but I'm interested in your stack.
It runs on a dart.
Odinachi David , hey i want start use dart for backend , but i am warraried about the hosting on dart , how does it work?
Pretty much like any other backend, you could host on aws or Heroku depending on your preference