Getting started with haXe/Neko
A simple console program with haXe and Neko
To create a simple console program in haXe, first you need to compile the source code to Neko bytecode and then execute it using the NekoVM, like the following:
- Create a haXe source file named 'Hello.hx', containing the following code:
class Hello { static function main() { neko.Lib.print("Hello World!"); } }
- Open up a Command Prompt window (Start -> Run -> enter
cmd.exein the text box), change the shell's current working directory to where you have saved Hello.hx file with thechdircommand, 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 your source code, you can use 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 by typing the following command:
haxe Hello.hxml
Finally, to execute/run your code, invoke the NekoVM with the name of the bytecode file, like this:
neko Hello.n
You can also convert your bytecode 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
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 a bytecode file from it. Create index.hxml so it contains the following content inside :
-neko index.n -main Index
You can now run the index.hxml by simply double-clicking on it on Windows, or, for other platforms, run the command haxe index.hxml in a terminal.
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 it is compatible across different OS and can be run using the Dev WebServer.
Using the WebServer
Now that you have the bytecode for your haXe code, let's run it and see what happens. 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 the targeted platforms. 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 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 Web class.