Monday, 10 February 2014

Run server on Random Ports | NodeJS

Here, server.address() returns bound address, the family address, and the port of the server. Given below, 51134 is the randomly generated port.

Server | server.address()


var net = require('net');
var server = net.createServer(function (socket) {
  socket.end("goodbye\n");
});

// grab a random port.
server.listen(function() {
  address = server.address();
  console.log("opened server on %j", address);
});


| Run Server Node Application.

OUTPUT |
opened server on {"address":"0.0.0.0","family":"IPv4","port":51134}


Client | net.connect(options, [connectionListener])


var net = require('net');
var client = net.connect({port: 51134},
  function() { //'connect' listener
  console.log('client connected');
  client.write('world!\r\n');
});
client.on('data', function(data) {
  console.log(data.toString());
  client.end();
});
client.on('end', function() {
  console.log('client disconnected');
});


| Now, run the Client Node Application.

OUTPUT | Client-side
client connected
goodbye
client disconnected

Client - Server connection | NodeJS

The net module lets you provide the network wrapper and creates both servers and clients.
Here, net.createServer(....) creates a new TCP server.

Server | net.createServer([options], [connectionListener])


var net = require('net');
var server = net.createServer(function(socket) { //'connection' listener
  console.log('server connected');
  socket.on('end', function() {
    console.log('server disconnected');
  });
  socket.write('Echo server\r\n');
  socket.pipe(socket);
});
server.listen(8124, function() { //'listening' listener
  console.log('server bound');
});


| Run Server Node Application.

OUTPUT |
server bound


Client | net.connect(options, [connectionListener])


var net = require('net');
var client = net.connect({port: 8124},
  function() { //'connect' listener
  console.log('client connected');
  client.write('world!\r\n');
});
client.on('data', function(data) {
  console.log(data.toString());
  client.end();
});
client.on('end', function() {
  console.log('client disconnected');
});


| Now, run the Client Node Application.

OUTPUT | Client-side
client connected
Echo server
world!
client disconnected


| Observe the Sever console

OUTPUT | Server-side
server bound
server connected
server disconnected

How to get started with Nodejs ?

Lets get started with 'Hello world' example

1| I use nodeclipse, a plugin from marketplace for Eclipse IDE.
2| Create a Nodejs project and copy the below snippet.

Snippet


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

OR|


var http = require('http');

http.createServer(function handler(request, response) {
    response.writeHead(200, {'Content-Type': 'text/plain'});
    response.write('Hello World\n');
    response.end();
}).listen(1335, '127.0.0.1');
console.log('Server running at http://127.0.0.1:1335/');


OUTPUT |










3| To stop the server, use Eclipse IDE, Run > Debug
4| Right-click and select Terminate option from the list.
5| Now, re-run the snippet.