0

I have seen some tutorials on how to run a webserver on the Dart VM on linux machine. But what are the basic steps for doing the same on a windows server? Would you need to turn off the ISS if that is running? I assume I would need to hook up the VM through some environmental variables, but I have not seen a tutorial.

MrMambo007
  • 133
  • 1
  • 7

1 Answers1

2

It's a different mental concept to something like IIS.

Essentially, you run a .dart script from the command line using the dart binary dart.exe

For example, the following script represents a "dart server" listening on port 8080

import 'dart:io';

void main() {
  var httpServer = new HttpServer();
  httpServer.defaultRequestHandler = (req, HttpResponse res) {
    var result = "${req.method}: ${req.path}"; 
    print(result);  // log to console  
    res.outputStream.writeString("You requested $result"); // return result to browser
    res.outputStream.close();
  };

  httpServer.listen("127.0.0.1", 8080);

}

Save the text above as myServer.dart and from the command line, run dart.exe myServer.dart.

Then navigate to http://127.0.0.1:8080/foo/bar and you will get the output below shown in the browser:

You requested GET: /foo/bar

From that, you can write code to add more handlers for specific methods / paths etc..., load files from the file system to send to the browser, access data sources, return data, anything, really that you can write in Dart code and send to the browser.

(Clarification: You would only need to turn of IIS if it is already serving on the same port, for this example, port 8080).

Chris Buckett
  • 13,738
  • 5
  • 39
  • 46
  • Thanks a lot. It worked locally on the server... Next step is to figure out how to make it respond to a real url on the web – MrMambo007 Feb 12 '13 at 20:25
  • the ISS server is currently listening to port 80. The 8080 is also a HTTP port so it might interfere – MrMambo007 Feb 12 '13 at 20:35
  • Feel free to use another number - 8081, 8000, 8001 are all good :). Take a look at this question for a bit more help re adding handlers: http://stackoverflow.com/questions/13713593/can-some-one-give-be-a-sample-code-how-to-add-a-request-handler-to-a-httpserver/ – Chris Buckett Feb 12 '13 at 21:29