Chapter 7. Server-side Applications with Dart

In this chapter, we'll take a look at developing command-line server-side apps. In particular, we'll learn the following topics:

  • How to process command-line arguments with the args package
  • Writing a small chat app with a WebSockets server in Dart
  • How to access filesystem with the dart:io library
  • Storing data to the MySQL database with the sqljocky package
  • Writing a small web server using the route package
  • How to use Apache and nginx servers as reverse proxies for Dart HTTP servers

This chapter assumes that you already have at least basic experience with CLI scripts or Unix environments in general. If you're a Windows user, take a look at Cygwin (www.cygwin.com), which is a Unix-like environment for Windows, or ideally, install Ubuntu into a virtual machine. The Dart SDK is now available for Debian and Ubuntu Linux distributions, while Dart Editor is currently available only for Ubuntu Linux.

We'll set off with a few notes about Dart VM and then go to processing command-line arguments, because that's what you'll probably use every time you start developing a CLI app.

The standalone Dart VM

The Dart SDK comes with a standalone Dart VM to run command-line apps such as pub or dart2js. You can find it in <dart-sdk-dir>/bin/dart. It accepts command-line arguments such as -h for help or -c to enable the checked mode. There are also special options for the Observatory tool (we'll use it in Chapter 8, Testing and Profiling the Dart Code). These aren't very interesting for us now, but feel free to check out https://www.dartlang.org/tools/dart-vm/ for a complete list of all supported options.

The standalone Dart VM is mostly the same environment as the VM implemented in the Dartium browser. The entry point for an app is a top-level main()function, but this time, it accepts a list of arguments passed from the command line:

main(List<String> args) {
  /* ... */
}

With the standalone Dart VM, you can't use libraries specific to the browser environment, such as dart:html, of course. On the other hand, you can use dart:io to access the filesystem or create subprocesses, listen to signals, and so on. We're going to use this package a lot in this chapter.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.191.144.65