HaXe Wiki
(Adding categories)
m (Haxe language is no longer spelled haXe)
Tags: Visual edit apiedit
 
(One intermediate revision by one other user not shown)
Line 1: Line 1:
==== A simple console program with haXe and Neko ====<br />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:<br /><br /> * Create a haXe source file named 'Hello.hx', containing the following code:<br />&lt;code haxe&gt;<br />class Hello {<br /> static function main() {<br /> neko.Lib.print("Hello World!");<br /> }<br />}<br />&lt;/code&gt;<br /><br /> * On Windows, open up a Command Prompt window (Start -&gt; Run -&gt; 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:<br />&lt;code haxe&gt;<br />haxe -main Hello -neko Hello.n<br />&lt;/code&gt;<br /><br />This instructs the haXe compiler to compile the haXe source into Neko bytecode and save the bytecode as the file Hello.n.<br /><br />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:<br /><br />&lt;code haxe&gt;<br />-main Hello<br />-neko Hello.n<br />&lt;/code&gt;<br /><br />The haXe code can now be compiled by typing the following command:<br /><br />&lt;code haxe&gt;<br />haxe Compile.hxml<br />&lt;/code&gt;<br /><br />Finally, to execute/run the compiled bytecode, invoke the NekoVM with the name of the bytecode file:<br /><br />&lt;code haxe&gt;<br />neko Hello.n<br />&lt;/code&gt;<br /><br />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''.<br /><br />&lt;code haxe&gt;<br />nekotools boot Hello.n<br />&lt;/code&gt;<br /><br />==== A simple webpage with haXe and Neko ====<br />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'' :<br /><br />&lt;code haxe&gt;<br />class Index {<br /> static function main() {<br /> trace("Hello World !");<br /> }<br />}<br />&lt;/code&gt;<br /><br />Now compile this sample and create a bytecode file from it. Create ''Compile.hxml'' so it contains the following content:<br /><br />&lt;code&gt;<br />-neko index.n<br />-main Index<br />&lt;/code&gt;<br /><br />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.<br /><br />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.<br /><br />====== Using the WebServer ======<br /><br />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.<br /><br />You can check that it's running correctly and configure it by visiting the [[http://localhost:2000/server:config|Configuration]] 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.<br /><br />You can now click the [[http://localhost:2000/|Visit]] link that will display ''Hello World'' with the filename and line number where the trace occured.<br /><br />====== Web API ======<br /><br />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 :<br /><br />&lt;code haxe&gt;<br />class Index {<br /> static function main() { <br /> neko.Lib.print(neko.Web.getParams());<br /> }<br />}<br />&lt;/code&gt;<br /><br />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.<br /><br />Recompile it by double-clicking the ''index.hxml'' again, and visit [[http://localhost:2000/?x=33;v=hello|This URL]]. It will display the request parameters for the URL that are sent by the browser.<br /><br />There is also a lot of useful functionality available in the [[/api/neko/Web]] class.<br />
+
==== 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 :<br /><br />&lt;code haxe&gt;<br />class Index {<br /> static function main() { <br /> neko.Lib.print(neko.Web.getParams());<br /> }<br />}<br />&lt;/code&gt;<br /><br />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.<br /><br />Recompile it by double-clicking the ''index.hxml'' again, and visit [[http://localhost:2000/?x=33;v=hello|This URL]]. It will display the request parameters for the URL that are sent by the browser.<br /><br />There is also a lot of useful functionality available in the [[/api/neko/Web]] class.<br />
 
[[Category:Neko]]
 
[[Category:Neko]]
 
[[Category:Tutorials]]
 
[[Category:Tutorials]]

Latest revision as of 11:31, 8 June 2015

==== 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 :

<code haxe>
class Index {
static function main() {
neko.Lib.print(neko.Web.getParams());
}
}
</code>

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 [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.