Templates by BIGtheme NET

Node.js Quick Start

In this article, I will show how to start quick development with Node.js.
How to Install Node.js on Windows.
How to create and run simple program that prints “Hello World!”.
How to create and test your own HTTP server with node.js.
How to connect to database (MySQL) and query to get table data.

Is Node.js Multi-thread ? – Answer is NO
Is Node.js a Javascript Framework ? – Answer is NO
Is Node.js a non-blocking Framework ? – Answer is YES

What is Node.js?

The main purpose of Node.js is for non-blocking and
all of it’s libraries are non-blocking.

Node.js is one of a popular server-side platform used to develop modern web applications.
Beauty is that, you can write everything in Javascript and able to run on any type of server
like Mac, Linux or Windows. But Node.js is not a Javascript framework.

If we are using Node.js, The Application logic will spread between front-end and back-end.
If You are using JSON (JavaScript Object Notation), With Node.js application this JSON
can be used all three areas of the application – client, server and database.

Before writing simple program, Node.js should be installed on your machine.

How to Installation Node.js?

Installation is very simple, Open the link nodejs,
and click on INSTALL button, then installer will b downloaded
(node-v0.12.5-x64 is the installer name). Once the installer is downloaded, follow given
steps to install Node.js.

1) Double click on the Installer, and click Next button.

2) Accept the terms in the License Agreement and click on Next button.

3) You can choose the path, default is C:\Program Files\nodejs\ and click on Next button.

4) You will be in “Custom Setup” screen, click on Next button.

5) You will be in “Ready to install Node.js” screen, click on Install button.

6) Click on Finish button.

finish screen

How to Write a simple Hello world Program?

Create simple file helloworld.js with single line content.

 console.log('Hello World!'); 

You can Download the helloworld.js file Here,
helloworld

How to run this helloworld.js with Node.js :

Open command prompt, type simple command
> node helloworld.js

This will print Hello World! on console.

helloworld

How to create your own HTTP server with Node.js?

Even we can create our own servers with Node.js.
In my example, I am creating a HTTP server to respond with “Hello World From HTTP server”
message to all my HTTP requests.

Create a HTTP server with Node.js :

Simple two steps to create a server,

1) Load required server using require(”) method of Node.js.

2) Configure the server with your own configurations.

Create File helloworld_FromHttpSever.js,

// To create HTTP server, we need to load the http module from NodeJS.
var http = require('http');

// Configure our HTTP server to respond with Hello World to all requests.
var server = http.createServer(
function (request, response) {
  response.writeHead(200, {"Content-Type": "text/plain"});
  response.end("Hello World\n");
});

// Listen on port 6148, IP defaults to 127.0.0.1
server.listen(6148);

// Put a friendly message on the terminal
console.log("Server running at http://127.0.0.1:6148/");

You can Download helloworld_FromHttpSever.js file source code,
Here helloworld_FromHttpSever

How to Run the server, given is the simple command to run the server.

> node helloworld_FromHttpSever.js

nodejs_http_server

Open any browser and access the url http://127.0.0.1:6148/
Then we can see, Hello World on the screen.
browser

How the request flow :

1) When ever you run First time helloworld_FromHttpSever.js – Node is going to register events,
In this case, event is HTTP request event.

2) Once you execute the code, node goes to request loop and continuously checking is the request is
HTTP request events or not. (This run in infinite loop, never ends until we stop manually).

3) As soon as HTTP request event comes, going to triggered that event and
run callback re-load and prints “Hello World”.

Here the callback function is, so these two lines of code will be executed for all HTTP requests.

function (request, response) {
  response.writeHead(200, {"Content-Type": "text/plain"});
  response.end("Hello World\n");
}

We can create server to interact with database and pull some data
for reporting purpose.

How to connects to Database and perform SQL query :

I am using MySQL database for demo.

Simple steps are,

1) You need MySQL module, downloads and installs MySQL module using given command,
npm install mysql@2.0.0-alpha2

This downloads and installs the module, and it also creates the node_modules/mysql
folder in the current directory.

2) Create simple program with name mysql_nodejs.js with given content,

Steps to connect and query database are,

(a) Create msql server.

(b) Create Database connect.

(c) Query the Database to pull table data.

(d) Close Database connect.

mysql_nodejs.js

// Create msql server
var mysql = require("mysql"); 

// Create a connection to the server.
var connection = mysql.createConnection({ 
	host: 'localhost', // Host name, in my case, it is 
	user: "DB User name", 
	password: "DB Password",
	database: "Data Base name"
});
// Create Database connect 
connection.connect();

// Query the Database to pull emp data
connection.query('SELECT * from emp', function(err, rows, fields) {
  if (!err)
    console.log('Emp table sontent is: \n', rows);
  else
    console.log('Error while performing Query.');
});

// Close Database connect 
connection.end();

You can download complete code Here,
mysql_nodejs

yoge

Run mysql_nodejs.js, then this will pull all the records from table emp,
db_run

*** Venkat – Happy leaning ****