Haxe Code Cookbook
Haxe programming cookbookJavaScriptCreate a server with Haxe/NodeJS

Create a server with Haxe/NodeJS

Reading time: 1 minute

The following tutorial creates a http server using Haxe/Node.js at port 8000 and demonstrates how to build and run it.

Haxe can compile to many targets, this example is specific for the Haxe/JavaScript target only.


  • Install Node.js on your machine.
  • Install hxnodejs with haxelib install hxnodejs (released version).
  • .. or install the latest version from github haxelib git hxnodejs https://github.com/HaxeFoundation/hxnodejs.

Structure of this example

+ example/
  - Main.hx
  - build.hxml
  - run.hxml


class Main {
  static function main() {
    // Configure our HTTP server to respond with Hello World to all requests.
    var server = js.node.Http.createServer(function(request, response) {
      response.writeHead(200, {"Content-Type": "text/plain"});
      response.end("Hello World\n");

    // Listen on port 8000, IP defaults to

    // Put a console.log on the terminal
    trace("Server running at");

Note that each Haxe application has a static entrypoint which is called main().

Compile the Haxe/node.js server

Compile the example with haxe -lib hxnodejs -main Main -js main.js. You can also save this build configuration in a build.hxml file:

-lib hxnodejs 
-main Main 
-js main.js

To run the build.hxml script, it can be passed to the Haxe compiler on the commandline: haxe build.hxml. Windows users can double click the hxml file to run it.

Run the Haxe/node.js server

To run the node application, use node main.js. Alternative, you can save this configuration in a run.hxml file.

-cmd node main.js

Then, load in a browser to see the server output.

Generated output

Haxe is an excellent JavaScript compiler, the output source is clean:

(function () { "use strict";
var Main = function() { };
Main.main = function() {
  var server = js_node_Http.createServer(function(request,response) {
    response.writeHead(200,{ "Content-Type" : "text/plain"});
    response.end("Hello World\n");
  console.log("Server running at");
var js_node_Http = require("http");

Related info:

Last modified:
Category:  JavaScript