Node JavaScript

Posted on Apr 12 2012 - 5:01am by Huzoor Bux

Node.js is a platform built on Chrome’s JavaScript run time for easily building scalable, fast network applications. Node.js uses an non-blocking, event-driven I/O model that makes it efficient and lightweight, perfect for data-intensive real-time applications that run across distributed devices.

AN EXAMPLE: WEBSERVER

This simple web server written in Node responds with “Hello Node” for every request.

var http = require(‘http’);
http.createServer(function (req, res) {
res.writeHead(200, {‘Content-Type’: ‘text/plain’});
res.end(‘Hello Node\n’);
}).listen(1337, ‘127.0.0.1’);
console.log(‘Server running at http://127.0.0.1:1337/’);

To run the server, put the code into a file example.js and execute it with the node program:

% node example.js
Server running at http://127.0.0.1:1337/

Here is an example of a simple TCP server which listens on port 1337 and echoes whatever you send it:

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’);

  • It’s a pity you don’t have a donate button! I’d most certainly donate to this fantastic blog! I suppose for now i’ll settle for book-marking and adding your RSS feed to my Google account. I look forward to fresh updates and will talk about this blog with my Facebook group. Chat soon!|