Running dart in a web server

Issue

How do I run dart in a server? say in localhost or any web server? Currently google provides a dart editor which executes the code in dartium browser. Also even if I get to run it on a server would it be visible to others viewing the page in a browser other than dartium?

Solution

When you create a new “Web Application” using the Dart Editor, it creates a .html file and a .dart file. The html file uses a tag to link to the .dart file, eg:

MyApp.html //contains <script type="application/dart" src="MyApp.dart"></script>
MyApp.dart //contains dart app code.

The editor can also generate a javascript file from the .dart file, eg:

MyApp.dart.js //contains dart app code converted to JS

As far as a web server is concerned, these are simply static files that get served to the browser.

The html file contains a link to a special JavaScript script which can identify if the browser being used has native support for Dart (ie, Dartium).

  • If it does, then then the MyApp.html and MyApp.dart pair of files is used.

  • If the browser does not support Dart natively, then the special script dynamically changes the script element to point to the MyApp.dart.js file instead, so that the browser receives the javascript version of your app.

This means that you can copy the three files (.html, .dart, .js) onto any web server (localhost or otherwise), and simply browse to the .html file.

For completeness, the “special script” can be viewed here:
http://dart.googlecode.com/svn/branches/bleeding_edge/dart/client/dart.js

Answered By – Chris Buckett

Answer Checked By – Gilberto Lyons (FlutterFixes Admin)

Leave a Reply

Your email address will not be published. Required fields are marked *