node.js memo

install:

sudo port install nodejs
sudo port install netcat

tcp echo server js:

var net = require("net");
net.createServer(function(sock){
    sock.on("data", function(data){
        sock.write(data);
    });
}).listen(8000);

http echo server js:

var http = require("http");
http.createServer(function(req, res){
    res.writeHead(200, {'Content-type' : 'text/plain'});
    res.write("Hello\n");
    res.end();
}).listen(8000);

test client command:

nc localhost 8000
curl http://localhost:8000