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?
4 Answers
You can actually run Dart as a web server:
import 'dart:io';
main() {
HttpServer.bind('127.0.0.1', 8080).then((server) {
server.listen((HttpRequest request) {
request.response.write('Hello, world');
request.response.close();
});
});
}
That starts a web server on the local machine using port 8080. It simply returns "Hello, world".
From there all you need to do is define your routes, actions, etc.
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
-
Chris, would it be possible to use your JSON Dart Example dartlang.org/articles/json-web-service to be served by a Apache or lighthttpd or nginx WebServer? btw, do you have the whole full code as one somewhere? ^^– GeroApr 28, 2012 at 13:29
-
Yes, that would work with any web server providing services via POST and GET. There is similar code in this example which sends and retrieves data from a web server: github.com/chrisbu/dartwatch-blog-server/blob/master/client/… (the server part in this instance is also written in dart, but it doesn't need to be) Apr 30, 2012 at 14:54
Dartium is only a Chromium Browser with the abilty to directly run dart in a "dart vm". This accelerates the development process. The common way to use dart in other browsers and on your webserver is: compile the dart-code to native javascript :)
http://www.dartlang.org/docs/getting-started/sdk/#frog
Frog is the compiler, which compiles dart-code to javascript
-
2The question was (as I think) in how to move dart from client side (browser) to server side Apr 28, 2012 at 5:42
This is how I put Dart on Google App Engine, that is: the Dart that is running in the browser / on the client side:
http://ambio-strong.blogspot.no/2012/07/dart-on-google-app-engine.html
-
2You might want to edit that answer, it points to a blogpost which itself points to sample code. Thats a triple redirection. Feel free to ping me for a upvote when done :)– eckesJul 23, 2012 at 23:29