Channels ▼


Building Node.js Projects in Visual Studio

Node.js Tools for Visual Studio ( NTVS), is an open-source Visual Studio plugin that transforms Visual Studio into a IDE for developing Node.js apps. Node.js has become extremely popular, as a server-side runtime environment for JavaScript apps; and this plugin brings the possibility for Visual Studio users to take advantage of their knowledge of the IDE to work with Node.js code. It includes IntelliSense, a Node.js interactive window, debugging and profiling, among other features. In this article, I provide an example of the usage of these features.

Writing Node.js code in Visual Studio and Using IntelliSense

You can download the latest version of Node.js Tools for Visual Studio here. At the time of writing, the latest version for Visual Studio 2013 was 1.0 Release Candidate. It is very stable and allows you to use Visual Studio 2013 as your IDE for Node.js projects without major problems. In the event you've worked with the previous versions of the plugin, you should upgrade because this release resolved many bugs and crashes. The project is developed and supported by Microsoft and the community. It is important to note that there is already a version that works with the Visual Studio 2015 preview, so you can expect that the forthcoming Visual Studio version will also allow you to use it as the IDE for Node.js projects.

Node.js Tools works with your existing Node.js installation. In fact, when you execute your Node.js project, the IDE will run the installed node.exe. The Visual Studio debugger uses the node V8 debugger under the hood, but you work with the IDE in the same way and with the same windows you are used to interacting with when coding in C# or other Visual Studio supported languages.

Once you install the tools, the New Project dialog box in Visual Studio provides the following new templates under Installed | Templates | JavaScript | Node.js:

  • Azure Cloud Service
  • From Existing Node.js code
  • Blank Node.js Console Application
  • Blank Node.js Web Application
  • Basic Node.js Express 3 Application
  • Starter Node.js Express 3 Application
  • Basic Node.js Express 4 Application
  • Blank Azure Node.js Web Application
  • Blank Azure Node.js Express 3 Application
  • Starter Azure Node.js Express 3 Application
  • Basic Azure Node.js Express 4 Application

Start a new project with the Blank Node.js Web Application template. You can use IntelliSense to replace the code in server.js with the following lines. This code uses the HTTP and URL libraries to create a very simple Node.js REST API that is capable of processing an HTTP GET status method with some parameters.

Listing One: Creating a simple REST API.

// Require the HTTP library
var http = require("http");
// Require the URL library
var url = require('url');

var port = process.env.port || 1337;

var server = http.createServer(function (request, response) {
    var parsedUrl = url.parse(request.url, true);
    var query = parsedUrl.query;
    if ((request.method == 'GET') && (parsedUrl.pathname == '/status')) {
        status(query, response);

function status(query, response) {
    var cameraToQuery = 0;
    var detectedFacesCount = 0;
    var globalStatus = 'N/A';
    var lensStatus = 'N/A';
    var environmentScore = 0;
    if (query.position) {
        switch (query.position) {
            case 'front': cameraToQuery = 1;
                globalStatus = 'OK';
                lensStatus = 'OK';
                environmentScore = 4;
                detectedFacesCount = 300;
            case 'back': cameraToQuery = 2;
                globalStatus = 'OK';
                lensStatus = 'OK';
                environmentScore = 3;
                detectedFacesCount = 150;
            case 'left': cameraToQuery = 3;
                globalStatus = 'OK';
                lensStatus = 'OK';
                environmentScore = 3;
                detectedFacesCount = 200;
            case 'right': cameraToQuery = 4;
                globalStatus = 'OK';
                lensStatus = 'OK';
                environmentScore = 2;
                detectedFacesCount = 170;
    console.log("Requesting status information for camera #: " + cameraToQuery);    
    // Write the response HEAD
    response.writeHead(200, {
        "Content-Type": "application/json",
        "Access-Control-Allow-Origin": "*",
        "Access-Control-Allow-Headers": "X-Requested-With"
    var obj = {
        cameraId: cameraToQuery,
        globalStatus: globalStatus,
        lensStatus: lensStatus,
        environmentScore: environmentScore,
        detectedFacesCount: detectedFacesCount, 
    // Write the response body
    // End of the response

// Start listening at port 1337 or the port number specified in process env port
console.log("Server is listening at port " + port);

As you might expect from your experience with Visual Studio, after you declare a variable, you can take advantage of IntelliSense to check the available methods and properties. For example, after you declare the http variable on line 2, you can check the methods available for http by typing the dot (.) after http in the following line (see Figure 1):

Figure 1: IntelliSense provides information about the available methods.

You will see the createServer function in the displayed list. When you select createServer, the IDE displays the documentation for the function. After you press Enter to auto-complete the http.createServer method and you open parenthesis to specify the arguments, the IDE displays the argument names list, in this case, requestListener.

The code in Listing One uses the HTTP and URL libraries to create a very simple Node.js REST API that is capable of processing an HTTP GET status method with some parameters. The server listens on port 1337 or the port specified in process.env.port. When the server receives an HTTP GET for the status method, it executes the status function. This function checks whether a position parameter value has been specified. The following are the four accepted values for position: front, back, left and right. The code converts the specified position value to a camera id (cameraToQuery) and generates values to simulate status data provided by cameras that detect and count faces in real-time. Then, the code sends the JSON response with values for the following keys: cameraId, globalStatus, lensStatus, environmentScore, and detectedFacesCount.

There are simpler ways to achieve the same goal by using other Node.js libraries, such as Express. However, I want to keep the sample code simple to allow you to understand the different features included in Node.js Tools for Visual Studio.

Select View | Other Windows | Node.js Interactive Window and the IDE will display the Node.js REPL (short for Read-Eval-Print Loop), which allows you to test code snippets. The interactive window supports IntelliSense.

Related Reading

More Insights

Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.