haXe

Getting started with haXe/Neko

A simple console program with haXe and Neko


To create a simple console program in haXe, compile it to Neko bytecode and then execute it using the NekoVM, do the following:

Create a haXe source file named 'Hello.hx', containing the following code:

class Hello {
    static function main() {
        neko.Lib.print("Hello World!");
    }
}

To compile it using the command-line, open up a command window (Start -> Run -> cmd), change the directory to where the Hello.hx file has been saved, and type the following command:

haxe -main Hello -neko Hello.n

This instructs the haXe compiler to compile the haXe source to Neko bytecode and to create the file Hello.n.

As an alternative to this style of compiling, you can create a .hxml file that contains directives to the haXe compiler. For the haXe code above, create a file named Hello.hxml containing the following:

-main Hello
-neko Hello.n

You can now compile the code with the following command:

haxe Hello.hxml

Finally, to execute/run your code, invoke the NekoVM passing it the name of the bytecode file, like so:

neko Hello.n

You can also convert your bytecode to a stand-alone executable by using the NekoTools tool. The command below will convert the bytecode Hello.n to Hello.exe, which can be executed/run directly.

NekoTools boot Hello.n

A simple webpage with haXe and Neko


You can also start developing websites 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 we need to compile this sample and create Bytecode for it. Create and edit the file index.hxml and put the following content inside :

-neko index.n
-main Index

Save and close. You can now run the index.hxml by simply double-clicking on it (on Windows, for other platforms run the command haxe index.hxml).

If everything worked well, it should create a file named index.n that contains the compiled haXe bytecode. This is a compiled portable form of the haXe code. It is compatible across different OS and can be run using the Dev WebServer.

Using the WebServer

Now that you have your bytecode, you need to be able to run it. If you're 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 your 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 Visit link that will display Hello World with the filename and line number where the trace occured.

Web API

So far we've been only using the haXe Generic API, available for all languages. Now let's look at the ServerSide specific API, which is located into the package neko. Modify your Index.hx file with the following content :

class Index {
    static function main() {  
        neko.Lib.print(neko.Web.getParams());
    }
}

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

Recompile, by running the index.hxml file and visit This URL. It will display the parameters sent in the URL by the browser.

A lot of useful functionality is available in the Web class.

version #1372, modified 2008-05-09 01:33:19 by Chax0