Search code examples
webserverdartwindows-server

How would I go about running the Dart VM on a windows server ?


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.


Solution

  • 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).