Getting started with Haxe/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:
    class Hello {
        static function main() {
            neko.Lib.print("Hello World!");
  • 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:
    haxe -main Hello -neko Hello.n

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:

-main Hello
-neko Hello.n

The Haxe code can now be compiled by typing the following command:

haxe Compile.hxml

Finally, to execute/run the compiled bytecode, invoke the NekoVM with the name of the bytecode file:

neko Hello.n

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.

nekotools boot Hello.n

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 :
class Index {
    static function main() {
        trace("Hello World !");

Now compile this sample and create a bytecode file from it. Create Compile.hxml so it contains the following content:

-neko index.n
-main Index

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 Neko Development WebServer to serve HTTP requests whose contents are generated by HaXe

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 Configuration Panel. Enter the directory where the index.n bytecode is located and press the Modify button to save your changes.

You can now click the Visit link that will display Hello World with the filename and line number where the trace occured.

If you don't want to change the directory each time you restart the server, you can add the -d parameter to your server start command. On windows, for example, the haxeserver.bat server start command could be changed to nekotools server -p 2000 -h localhost -d c:\your\web\root/.

For a more advanced

neko.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 :

class Index {
    static function main() {  

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 a lot of useful functionality available in the neko.Web class. Here are some examples:

  • getURI()
  • getHostName()
  • getMethod() - GET, POST, PUT, DELETE etc.
  • getClientHeaders()

The complete list of methods can be found in the neko.Web API doucmentation


For a more advanced web API example, make sure to check out the haxe.Web.Dispatch documentation. It will let you define an advanced web API using macros to get finegrained control of method access etc.

Reading user input

( since haxe 2.09 )

 * A simple exemple to demonstrate how to
 * read and write from and to the command line.
 * compile with haxe : > haxe -Main -neko main.n
 * execute with neko: > neko main.n
class Main {
    static function main() {
        // print a message on the screen
        Sys.println("What's your name?");
        // read user input
        var input = Sys.stdin().readLine();
        // print the result
        Sys.println("Hello " + input);
version #15725, modified 2012-11-22 18:30:07 by AndyLi