8

Is there a way to run Dart code on a server, similar to how Node.js runs javascript or ruby interpreter runs ruby code? Or is it that currently it can only run in Dartium?

snitko
  • 9,407
  • 10
  • 59
  • 89
  • 1
    See a previous similar question: http://stackoverflow.com/questions/10360855/running-dart-in-a-web-server – Ugtemlhrshrwzf May 04 '12 at 08:53
  • Yes, I've seen it. However, it still didn't answer the question. Or, it looks like the answer is no, and dart code can only be run in Dartium, but there is no stand-alone virtual machine to be used at the moment. Is that so or am I not understanding it correctly? – snitko May 04 '12 at 10:51
  • Check this thread: [Is there Dart VM available?](http://stackoverflow.com/q/7714302/648313) – Idolon May 04 '12 at 14:23

2 Answers2

10

The answer is yes.

For example, the following file Hello.dart:

main() => print("Hello World");

when run with the command (on windows, but also available for mac, linux)

dart.exe Hello.dart

will output

"Hello World"

It is very much like node.js.

Also, from the Dart Editor, you can click "New > Server App" and then the "run" command will work like the example above

Take a look at this file which runs an http server from the command line.

Update: I've written a blog post about this now, which should give an example, and runnable code

Chris Buckett
  • 11,013
  • 2
  • 34
  • 45
  • 1
    Bucket correct url on blog post: http://blog.dartwatch.com/2012/05/there-have-been-number-of-posts-on.html – Helpa Jul 02 '12 at 19:16
3

Yes, you can run server-side applications written in Dart. The Dart project provides a dart:io library that contains classes and interfaces for sockets, HTTP servers, files, and directories.

A good example of a simple HTTP server written in Dart: http://www.dartlang.org/articles/io/

Sample code:

#import('dart:io');

main() {
  var server = new HttpServer();
  server.listen('127.0.0.1', 8080);
  server.defaultRequestHandler = (HttpRequest request, HttpResponse response) {
    response.outputStream.write('Hello, world'.charCodes());
    response.outputStream.close();
  };
}
Seth Ladd
  • 77,313
  • 56
  • 165
  • 258