Getting Started With Node.js in Visual Studio

Introduction

 
Node.js was created by Ryan Dahl starting in 2009. It is one of the fastest-growing platforms for building scalable web applications. This article explains how to begin using Node in a console, its background history. You can download the Node.js framework onto your system.
 

What is Node.js?

 
Node.js is a server-side JavaScript. Node.js is a platform built on Chrome's JavaScript runtime for easily building fast and scalable web applications. Node.js uses an event-driven, non-blocking I/O model that makes it lightweight, efficient and perfect for data-intensive real-time applications that run across the distributed devices. Node.js is event-driven, but most servers are thread-based.
 

Why Node.js?

 
Node.js is specially designed for building fast, efficient, scalable network applications and uses an event-driven, non-blocking I/O model to maximize efficiency. The callback function is what Node calls a listener function and it is called by the server whenever a new request comes in.
 
How it works?
 
The following describes how Node.js works:
  • You use your web browser to make a request for "about.html" on a Node.js web server.
  • The Node server accepts your request and calls a function to retrieve that file from disk.
  • While the Node server is waiting for the file to be retrieved, it services the next web request.
  • When the file is retrieved, there is a callback function inserted into the Node server's queue.
  • The Node server executes that function, that in this case would render the HTML page and send it back to your browser.
Platforms for Node.js
  • Runs on OSX, Linux, Windows.
  • Clickable installer for Windows and MacOSX.
Open the editor, for example, Notepad and write the simple Hello World and save the file with a ".js" extension. You can run this by putting it in a file called "HelloWorld.js" and running it with the node "HelloWorld.js".
 
The console.log() functions are synchronous when the destination is a terminal and prints to values with new line log functions that can take multiple arguments, such as like in a printf().
 
console.log("value :%d",value)
 
Step 1
 
Hello
 
Step 2
 
Now open the Node.js Command prompt and execute the simple hello world as in the following figure.
 
OpneCmd
 
Welcome
 
Add two numbers in the console using Node.js
 
When you need to convert strings to real numbers you use the ParseInt() or ParseFloat() functions, but a faster way is to subtract 0 since subtracting from a string is not possible; V8 tries to treat the content of the string as a number.
 
Step 1
 
Open an editor, for example, Notepad and write the following code and save the file with a ".js" extension.
 
AddTwoValues
 
Step 2
 
Now open the Node.js Command prompt and execute the file as in the following figure.
 
Addition
 
Integrate Node.js Tools with Visual Studio2012
 
You can install the Node.js Tools as a Visual Studio 2012 Tool. Just click on this link Integrate Node.js Tools with Visual Studio2012. Then after installing Node.js Tools into Visual Studio if you are getting an error such as "Object reference not set to an instance of an object" then you need to update your Visual Studio through this link Update Visual Studio2012.
 
Use the following procedure to create the new website for Node.js in Visual Studio.
 
Step 1
 
Open Visual Studio then select "File" then select "New Website" then select "JavaScript Template" --> "Blank Node.js.Web Application" as in the following figure.
 
CreateNewNode
 
Step 2
 
Now the application will appear as in the following figure:
 
NodeWebApp
 
Step 3
 
Now debug the application or press F5 and the output will be shown in a browser as in the following figure:
 
Debugging
 

Summary

 
Node.js Tools for Visual Studio includes complete support for debugging Node applications. This includes stepping, breakpoints, break on exceptions, as well as locals, watch, immediate and call stack pool windows.