node.js
What is node.js?
Server side JavaScript, running on top of Google's V8 JavaScript engine.
Node.js is a platform built on Chrome's JavaScript runtime for easily building fast, scalable network applications. Node.js uses an event-driven, non-blocking I/O model that makes it lightweight and efficient, perfect for data-intensive real-time applications that run across distributed devices.
NOTE: node.js outperforms Python and Ruby, thanks to the V8 engine.
Install (from github repository)
Prerequisites (Unix only):
- Python 2.6 or 2.7
- GNU Make 3.81 or newer
- libexecinfo (FreeBSD and OpenBSD only)
Unix/Macintosh
./configure make make install
Windows
vcbuild.bat
Simple node.js web server
This simple web server written in Node responds with "Hello World" for every request.
Create server.js with the following code
var http = require("http"); http.createServer(function(request, response) { response.writeHead(200, {"Content-Type": "text/plain"}); response.write("Hello World\n"); response.end(); }).listen(8888); console.log('Server running on port 8888');
To run the server, put the code into a file example.js and execute it with the node program
% node server.js Server running
Echo Server
echo server in node.js
var net = require('net'); var server = net.createServer(function (socket) { socket.write('Echo server\r\n'); socket.pipe(socket); }); server.listen(1337, '127.0.0.1');
Simple TCP server which listens on port 1337 and echoes whatever you send it.
Example use case (shadowsocks-nodejs)
Pull all files on to both server and client
git clone https://github.com/clowwindy/shadowsocks-nodejs.git
Edit config.json
Run the proxy on the server
nohup node server.js > shadowsocks-nodejs.log 2>&1 &
Run
node local.js
Change browser, chat client proxy to localhost and the port specified in config.json.
NOTE: this is similar to SSH Tunnel but the advantage is that you don't have to reestablish the connection after switching WiFi or wired networks.
Reference
GitHub Project https://github.com/joyent/node
Node Version Manager (NVM) https://github.com/creationix/nvm