F.E.L.T.

Functionally Equivalent Language Translation

Coding for Node.js

First of all pleas make sure that you have read the PHP and JavaScript pages! The PHP guidelines *should be considered mandatory reading8 as they will give a good grounding in how FELT programming should be done. The JavaScript pages then contain those things are are different from PHP and finally, grasshopper, this page will tell you the differences that exist between the Node.js output mode and the JavaScript output mode.

And to be honest, there really aren't that many things to consider.

Node.js *is JavaScript8 as far as the syntax goes and therefore everything that holds for the JavaScript guidelines also holds for Node.js too. There are only two significant differences to take on board and that's it!

EMIT to Console

In Node.js mode the EMIT instruction writes to the console which is "stdout" when running Node.js applications from the command line. This is the most useful and obvious course of action and is why FELT makes that assumption.

You can see this for yourself in the REPL when you select Node.js as the target, before you click the render or execute buttons, using Chrome or Firefox etc, use the developer tools to show the JavaScript console and you will see your messages coming out there.

Using either JavaScript mode or Node.js modes means that you can learn FELT and try it out in the comfort of your own browser!

Including Files

The second important different is the way in which the REQUIRE instruction is interpreted bu the back-end coder. We have seen that PHP uses it in the normal PHP way and the JavaScript code uses it to create a `<SCRIPT.... > element for including a file. In the Node.js coder however there is a clash because in Node.js there is actually a real instruction called 'require'!

The back-end coder takes this into account and generates code that works as you you would expect it to allowing the usual Node.js idiom of assigning the return value from the require statement into a variable.

As an example, here is an example taken from page 27 of the book "Node Web Development" by PACKT publishing:

(defvar http (require :http))

(->
 (http.createServer
  (defun* (req res)
      (res.writeHead 200 (alist :Content-Type :text/plain))
    (res.end "Hello World!")))
 (listen 8124 :127.0.0.1))

(console.log "Server running at http://127.0.0.1")

It is the simplest example in the book that shows how to get a web server up and running with Node.js, now let us view the rendered code:

$ felt nodeserver.felt  -as nodejscoder
var http = require('http');;
http.createServer(function (req, res)
{res.writeHead(200, {'Content-Type' : 'text/plain'});
res.end("Hello World!");}
).listen(8124, '127.0.0.1');
console.log("Server running at http://127.0.0.1");

It may look a little odd but if you compare that to the example given in that book you will find it pretty close to identical and guess what, it just runs and does what you would expect:

$ felt nodeserver.felt  -as nodejscoder | node
felt nodeserver.felt  -as nodejscoder | node
Server running at http://127.0.0.1

And if you now point your browser at http://127.0.0.1:8124 you will find the message waiting there for you to read it.

So there you go... full Node.js development can be done with FELT as well. Obviously you will need to manage your code as pointed out before, and ensure that all functions named are available at run-time to make it all work.