Thursday, January 5, 2017

Starting with Node Js

Installing node.js in Virtual Machine.

After running the virtual machine, ssh to it.

I am using the NVM(Node Version Manager) to install the node. So we can install the version we want.

Anyway I have just installed the latest version.

First we need to install the NVM.

So take the terminal and follow the steps in the below link.

so after installing nvm we can install the desired node js version

I have installed the latest version  by using the below command

nvm install node

for more info about installing node check the below link:

After restarting the terminal if you see any error when running the command 'node' from the terminal like the following :

"The program 'node' can be found in the following packages:
 * node
 * nodejs
Try: apt-get install <selected package>"

then use the command : nvm use node

As I am using Virtual machine I need to update the server code slightly to get the node run in the main browser that is not in virtual machine

I have created the server using the below code.. check the listen section, where I put the IP address of the virtual machine.

var http = require("http");

http.createServer(function (request, response) {

   // Send the HTTP header 
   // HTTP Status: 200 : OK
   // Content Type: text/plain
   response.writeHead(200, {'Content-Type': 'text/plain'});
   // Send the response body as "Hello World"
   response.end('Hello World\n');
}).listen(8081, '');

// Console will print the message

console.log('Server running at');

Now you can start develop your own code..

No comments:

Post a Comment