Skip to content

erixtekila/nodejs-haxetelier

Repository files navigation

nodejs-haxetelier

Use node.js in conjunction with haxe (http://www.silexlabs.org/179221/the-blog/haxetelier4-passez-en-mode-node-js-pour-un-mhaxe-de-simplicite/)

Why node ?

  • While there are some interpreted language that runs inside a web server (think php with apache), node.js is a webserver

  • node.js provide an event driven model to create high performance web sites : the javascript runtime exposes event callbacks

  • A single eventloop treats all requests, no threads (unlike Apache...)

  • All IO is asynchronous, meaning that don't block the whole server

  • Node.js is perfect for high demand sites with low CPU usage

  • JSON is a first citizen format

  • Use javascript on both sides of the wire

  • Scale well

  • Sockets and websockets

  • ... hm, because !

Node.js primer

Mode Read-Evaluate-Print-Loop

Node.js can be used not only as a server, but also as a runtime environment.

In order to launch the REPL mode, one can issue from the command line :

node

It'll open an interactive shell exposing the Google's V8 javascript runtime.

Let's type some javascript atrocities...

undefined == null //true
"Why am I a " + typeof + ""; 
// "Why am I a number" (courtesy of http://www.wtfjs.com)

Some hints :

  • .help

  • .exit

Simple http server

Example 00-simple_http

TCP socket

Example 01-telnetchat

Use the following command line to chat :

telnet 127.0.0.1 1234

By the way, watch out for deconnections

events.js:72
        throw er; // Unhandled 'error' event
              ^

Error: This socket has been ended by the other party
    at Socket.writeAfterFIN [as write] (net.js:276:12)
    at /Volumes/soto/eric/Taf/formation/node.js/haxetelier/lab/01-telnet_chat/main.js:32:10
    at Array.forEach (native)
    at Socket.<anonymous> (/Volumes/soto/eric/Taf/formation/node.js/haxetelier/lab/01-telnet_chat/main.js:25:13)
    at Socket.EventEmitter.emit (events.js:95:17)
    at Socket.<anonymous> (_stream_readable.js:736:14)
    at Socket.EventEmitter.emit (events.js:92:17)
    at emitReadable_ (_stream_readable.js:408:10)
    at emitReadable (_stream_readable.js:404:5)
    at readableAddChunk (_stream_readable.js:165:9)

Express the node.js MVC framework

Example 02-express

sudo npm install express

then

sudo node main.js

Example 03-express_router

In order to send a POST HTTP message, one could use

curl -i -X POST -H 'Content-Type: application/json' -d '{"name": "Alexandre Hoyau", "pseudo":"ex"}' http://localhost/subscribe

or a REST client like the one in intelliJ IDEA

or create a node.js HTTP client