Saturday, 29 August 2015

Start Working With NodeJS And SqlLite On Windows using Visual Studio


This article expects that you have at least a minimal understanding of .NET development and object-oriented programming concepts. With a heavy focus on web development,this article also assumes that you have a basic understanding of ASP.NET, including the core concepts of web development contained in ASP.NET, such as clients and servers,HTML, CSS, JavaScript, and HTTP post/get.
The article also assumes that you have an IDE in which to work, specifically a fairly recent version of Visual Studio.

Source code:


Well, I believe the first thing which comes in mind here is what is nodeJs and why nodeJS?
If you have working knowledge of JavaScript and some other server side technology like ASP.NET,PHP or JSP then definitely you will understand why nodeJS?

What is NodeJS

Till now we had a concept like javascript is meant for client side scripting only but 'NO'. JavaScript has server side version called nodeJS which behaves or is like other server side technologies like,php,jsp .Node.js  but allows you to run JavaScript code in the back end, outside a browser. In order to execute the JavaScript you intend to run in the back end, it needs to be interpreted and, well, executed. This is what Node.js does, by making use of Google's V8 VM, the same run-time environment for JavaScript that Google Chrome uses.

Why NodeJS

Simple, Node.js has great capability when implementing real-time web applications using push technology over web-sockets. What is great about it? It is almost after 2 decades we have worked with stateless of web application, we finally have web applications with real-time, two-way connections, where both the client and server can initiate communication, allowing them to exchange data freely. This is in contrast to the typical web response pattern , where the client always initiates communication. Apart, it’s all based on the open web stack (HTML, CSS and JS) running over the standard port 80.all free :)
Plus, Node.js ships with a lot of useful modules, so you don't have to write everything from scratch, like for example something that outputs a string on the console.
If javascript runs on server side the question arises here is how to install environment to start with  nodeJS .Here are the ways you can setup or install nodeJS
So you can say nodeJS technology is all about real-time applications

Features of Node.js

Following are some of the important features that make Node.js the first choice of software architects.
  • Asynchronous and Event Driven All APIs of Node.js library are asynchronous that is, non-blocking. It essentially means a Node.js based server never waits for an API to return data. The server moves to the next API after calling it and a notification mechanism of Events of Node.js helps the server to get a response from the previous API call.
  • Very Fast Being built on Google Chrome's V8 JavaScript Engine, Node.js library is very fast in code execution.
  • Single Threaded but Highly Scalable - Node.js uses a single threaded model with event looping. Event mechanism helps the server to respond in a non-blocking way and makes the server highly scalable as opposed to traditional servers which create limited threads to handle requests. Node.js uses a single threaded program and the same program can provide service to a much larger number of requests than traditional servers like Apache HTTP Server.
  • No Buffering - Node.js applications never buffer any data. These applications simply output the data in chunks.
  • License - Node.js is released under the MIT license

Install Node.js Tools for Visual Studio

Available Releases
  • NTVS 1.1 Beta includes support for Visual Studio 2012, 2013, and 2015 RC
  • NTVS 1.0 includes support for Visual Studio 2012 and 2013
  • Dev Builds include the most recent changes and bugfixes (available for Visual Studio 2012, 2013, and 2015)
  • Older NTVS releases are also available.

Installation Steps:

STEP1 download windows installer on basis of your visual studio version you have from above location .once downloaded you will find below installer like shown below

Click on above windows installer, you will see below screen. Accept terms and click Next

STEP3 NTVS requires a Node.js interpreter to be installed. You can use a global installation of Node.js or you can specify the path to a local interpreter in each of your Node.js projects.
Node.js is built for 32-bit and 64-bit architectures. NTVS supports both. Only one is required and the Node.js installer only supports one being installed at a time. NTVS works with Node.js v0.10.20 or later.

Package management

npm is the pre-installed package manager for the Node.js server platform exactly like Nget Package Manager for .Net IDE environment. It is used to install Node.js programs from the npm registry. By organizing the installation and management of third-party Node.js programs, it helps developers build faster. The packages found in the npm registry can range from simple helper libraries like Underscore.js to task runners like Grunt.
Below is the screen shot of NPM

Start a new Node.js project in Visual Studio

Starting a new Node.js project like shown below
  1. Start Visual Studio.On the File menu, click New, and then click Project like other type of projects.
  2. In the New Project window, expand the Installed menu selection, expand Templates, and click JavaScript. In the main window, select Blank Node.js Web Application. Now your choice of choosing a path and name for your project, and then click OK.

  3. You will be presented with the following screen. You will SEE server.js file in the Solution Explorer .You will see sample or default "Hello world" application  like shown below
  4. You can now debug your node.js web application in your preferred browser like shown below.

  5. OUTPUT will be like

LET US ANALYZE A DEFAULT  "Hello World" app in Node.js

Here’s the code used in the server.js file

The generated code example or sample program shows you how to output "Hello World" in the browser. Let me explain how the generated code in server.js works line by line. 

Line 1

var http = require('http');
Node.js has a simple module and dependencies loading system. You simply call the function "require" with the path of the file or directory containing the module you would like to load at which point a variable is returned containing all the exported functions of that module.

Line 2

var port = process.env.port || 1337;
On this line, we want to determine on which port the HTTP server serving the HTML should run. If a port number is specified in the environment variables, we will use that one or we will simply use 1337.

Line 3

http.createServer(function (req, res) {
We want to create a server to handle HTTP requests. We will also pass the createServer function a function callback containing two parameters to a handle each individual request and return a response.   if you’ve never encountered callback functions in JavaScript. The request received is passed in the req parameter and the response is expected to written to the res parameter.

Line 4

res.writeHead(200, { '
Content-Type’: '
text/plain’ });
Any HTTP response requires a status-line and headers, . In this case, we want to return 200 OK as the status response and to specify the content-type as plain text. We specify this by calling the writeHead function on the response object.

Line 5

Hello World\n’);
Once we are done writing the response we want to call the end function. We can also pass the final content through the end function, in this case we want to send the string "Hello World" in plain text.

Line 6

We close off the callback and call the function listen at the port we defined earlier, this will start the server and start accepting requests sent to the defined port.
To see the result, you can start debugging by pressing on the button shown in the previous screenshot. You can see "Hello World" in the browser.

 You have now successfully run a Node.js app on Windows 8.1 using Visual Studio 2013.


For Simplicity I am going to use SQLLite database . you can use any database here is the explanation Database Drivers
SQLite is transaction database engine, it means you can run DDL and DML queries of SQL. DDL queries deal with creation of database, tables, view etc where as DML queries are responsible for CRUD operation (insert,delete,update) etc. Here is a code to create SQLite database and performing SQL operations.


Go to nodejs console and run

Modify existing sample code with below line

Just Click on Debug/Run in visual studio and the output will be like shown below

To run and analyse the code, you can simply use breakpoints in visual studio and see whether it creates Employee_Master table  and add two columns[EmployeeId,EmployeeName) with some 10  numbers of rows. You can view number of rows from  console because console.log is displaying all the rows from created table
if you will open sqllite database using admin tool you will see below screen with table name Emplyee_Master


    More learning for Node on Azure

    For more in-depth learning on node, my course is available here on Microsoft Virtual Academy.