Getting Started (Neko)

A simple console program with Haxe and Neko
☃☃To create and execute a simple console program in Haxe, the Haxe source code first needs to be created, then the source code needs to be compiled to Neko bytecode and finally the Neko bytecode needs to be executed using the NekoVM:☃☃☃☃ * Create a Haxe source file named 'Hello.hx', containing the following code:☃☃☃☃code haxe☃☃☃☃class Hello {☃☃   static function main {☃☃        neko.Lib.print("Hello World!");☃☃    }☃☃}☃☃☃☃/code☃☃☃☃☃☃ * On Windows, open up a Command Prompt window (Start -☃☃ Run -☃☃ enter cmd.exe in the text box), change the shell's current working directory to where the Hello.hx file has been saved with the chdir command, and type the following command to compile Hello.hx:☃☃☃☃code haxe☃☃☃☃haxe -main Hello -neko Hello.n☃☃☃☃/code☃☃☃☃☃☃This instructs the Haxe compiler to compile the Haxe source into Neko bytecode and save the bytecode as the file Hello.n.☃☃☃☃As an alternative way to compile the source code, a .hxml file can be used that contains directives to the Haxe compiler. For compiling the Haxe code above, create a file named Compile.hxml containing the following:☃☃☃☃☃☃code haxe☃☃☃☃-main Hello☃☃-neko Hello.n☃☃☃☃/code☃☃☃☃☃☃The Haxe code can now be compiled by typing the following command:☃☃☃☃☃☃code haxe☃☃☃☃haxe Compile.hxml☃☃☃☃/code☃☃☃☃☃☃Finally, to execute/run the compiled bytecode, invoke the NekoVM with the name of the bytecode file:☃☃☃☃☃☃code haxe☃☃☃☃neko Hello.n☃☃☃☃/code☃☃☃☃☃☃The bytecode can also be converted into a stand-alone executable by using nekotools. The command below will convert the bytecode Hello.n to Hello.exe, which can be executed/run directly, without the help of neko.exe.☃☃☃☃☃☃code haxe☃☃☃☃nekotools boot Hello.n☃☃☃☃/code☃☃☃☃☃☃==== A simple webpage with Haxe and Neko ====☃☃Websites can also be developed very quickly with Haxe. Let's start with a simple HelloWorld sample. Put the following code in the file Index.hx☃☃:☃☃☃☃☃☃code haxe☃☃☃☃class Index {☃☃   static function main {☃☃        trace("Hello World !");☃☃    }☃☃}☃☃☃☃/code☃☃☃☃☃☃Now compile this sample and create a bytecode file from it. Create Compile.hxml so it contains the following content:☃☃☃☃☃☃code☃☃☃☃-neko index.n☃☃-main Index☃☃☃☃/code☃☃☃☃☃☃The Compile.hxml can be run by simply double-clicking on it on Windows, or, for other platforms, run the command haxe Compile.hxml in a terminal window.☃☃☃☃If everything goes well, it should create a file named index.n that contains compiled Haxe bytecode. This is a portable form of the Haxe code in the sense that it is compatible across different OS and can be run using the Dev WebServer.☃☃☃☃====== Using the WebServer ======☃☃☃☃Now that the bytecode for the Haxe source code exists, run it and see what happens. On Windows, start the haxeserver.bat file that is in the Haxe installation directory. On other platforms run the command nekotools server. This will start the Haxe Development WebServer.☃☃☃☃You can check that it's running correctly and configure it by visiting the [☃☃] Panel. Enter the directory where the index.n bytecode is located and press the Modify button to save your changes. You need to change this directory everytime you restart the server, so leave it open.☃☃☃☃You can now click the [☃☃] link that will display Hello World with the filename and line number where the trace occured.☃☃☃☃====== Web API ======☃☃☃☃So far only the Haxe Generic API has been used, which is available for all the targeted platforms. Now let's look at the ServerSide specific API, which is located into the package neko. Modify the Index.hx file with the following content :

&lt;code haxe&gt; class Index { static function main { neko.Lib.print(neko.Web.getParams); } } &lt;/code&gt;

This will use the neko.Lib.print function that prints some raw strings (without adding debug information). It will print the parameters sent by the browser.

Recompile it by double-clicking the index.hxml again, and visit [|This URL]. It will display the request parameters for the URL that are sent by the browser.

There is also a lot of useful functionality available in the /api/neko/Web class.