The following two tabs change content below.
Hi, I have written and developed this site to share my experience and ideas with other colleagues. I also started to prepare interview questions and answers for job seekers. I hope it will help you a lot.

On Window System:

  1. Download Node.js on your system from https://nodejs.org/en/download/ according to your Operating System.
  2. After install set the path variable till \bin folder. In my case I have set like below:

path

3. Now for testing open command prompt (cmd) and create a file welcome.js on any specific location like:

api

4. Now for test the created program like:

nodejs

5. If your program run without any error means all are set. And now you can start your development.

 

On Linux System: 

1. Download the binary node.js setup file “node-v0.12.0-linux-x86.tar.gz” on /tmp location and install Node.js like           below.

$sudo apt-get install nodejs

2. Now set the path variable like window using below command.

$export PATH=$PATH:/usr/local/nodejs/bin

3. After all these steps we can create a sample program to test the installation is done successfully or not.
4. Create a directory on any specific location like below:

nodejs_l

5. Now create a Node.js program under this directory and test like below:

nodejs_test

 

Node.js program for database connection and API

Download program file from here db_and_api and code also given here

//  Database and API code

var express = require(‘express’);
var app = express();
var mysql = require(‘mysql’);

var connectionpool = mysql.createPool(
{
host : ‘localhost’,
user : ‘root’,
password : ”,
database : ‘nodejs’,
}
);

//API and Query for getEmployeeDetail start
var query = ‘SELECT * FROM employee’;
var rows;
app.get(‘/getEmployeeDetail’, function (req, res) {
connectionpool.getConnection(function(err, connection) {
if (err) {
console.error(‘CONNECTION error: ‘,err);
res.statusCode = 503;
res.send({
result: ‘error’,
err: err.code
});
} else {
connection.query(query, function(err, rows, fields) {
if (err) {
console.error(err);
res.statusCode = 500;
res.send({
result: ‘error’,
err: err.code
});
}
res.send({
result: ‘success’,
err: ”,
json: rows,
length: rows.length
});
connection.release();
});
}
});
})
//getEmployeeDetail End

var server = app.listen(8081, function () {
var host = server.address().address
var port = server.address().port
console.log(“Server listening at http://%s:%s”, host, port)
})

Some screenshots are here:

Node.js server start

db_api_start

 

API access and output

nodejs_output

399 total views, 4 views today

One thought on “How to install and setup the Node.js on window and Linux system”

Leave a Reply

Your email address will not be published. Required fields are marked *