Wednesday, August 31, 2016

How to install NodeJS on Lubuntu 16.04

How to install NodeJS on Lubuntu 16.04

If you are a javascript developer and you want to do some backend programming without having to learn other programming language then nodejs is the answer. This article i will show you how to install nodejs on lubuntu 16.04.

Nodejs is basically a javascript that runs on the backend (server) so it doesn't depend on a web browser to execute the code like the normal javascript do. And with nodejs you don't really need a web server, nodejs can have it's own web server.

Alright enough talking, let's install nodejs and i'll show you what it can do. To install nodejs on lubuntu you need to open your terminal/console/command prompt (press CTRL + ALT + T) and then run this command:

sudo apt-get install nodejs

Now that we have nodejs installed on lubuntu, let's try executing javascript file, let's create a helloworld program shall we? but before that if you want to know which nodejs version that you have, you can run this command:

nodejs -v

How to create NodeJS hello world program
Go ahead create a file called helloworld.js, inside that file we are just going to echo a string 'Hello World!', just like this:

console.log('Hello World!');

and then run the helloworld.js with this command:

nodejs helloworld.js

How to create a web server with NodeJS
Let's go even further, let's create a simple basic web server with nodejs, like i said before nodejs can have it's own web server and that's what we are going to make.

STEP 1, Create a new javascript file called myserver.js, on lubuntu you can use leafpad like this:

leafpad myserver.js

STEP 2, Copy paste this code into myserver.js :

var http = require('http');

var server = http.createServer(function(request, response) {
 console.log('incoming request');
 response.write('welcome to my web server \n');
        response.write(' is awesome blog');


STEP 3, Run the web server

nodejs myserver.js

STEP 4, Open url http://localhost:9999/ with your favorite web browser and feel the magic.

~ happy coding ~

No comments:

Post a Comment