Building a web service with Node.js in Visual Studio Part 1: foundations


I’ll be honest with you right from the start: I don’t like JavaScript too much. I don’t like its loose types, the lack of OOP features – although you can mimic them to some extent -, that everything seems to be a function which can be passed around, etc., I could go on and on. Keep in mind that this is only my personal opinion, you are free to like JS and its derivatives, like Angular.js, jQuery, Prototype, Knockout and about a million more. Admittedly, the web would be very different without these client-side technologies so I don’t wish them dead.

Node.js, however, can be more appealing to someone who’s more into back-end development, like me. It still has a funny syntax and loose types and all that but it is used to build server-side code. It is a server-side platform to build fast, low level, asynchronous and highly scalable web-based applications. I won’t regurgitate its basic characteristics, you can read about it on Wikipedia. One thing to note is that we write JS code to build applications for the Node.js platform but Node itself was not written in JavaScript.

As a .NET developer you may think you’ll never need another platform if you can go with .NET. That’s until the project leader tells you that the new API will need to run on Linux instead of Windows. Even in that case you’ll have several options, such as Java, Python or Ruby, but JavaScript is something that most web programmers come across at some point so it doesn’t take a long time to get started with it even if you haven’t worked with Node.js before.

Also, it’s always fun to learn something new and broaden your knowledge. Having at least a basic knowledge of Node.js will definitely boost your skill set. I think it’s always beneficial for a developer to look around and see what’s available out there. It helps you get out of your comfort zone and you’ll be able to propose 2-3 different solutions as new questions arise with their pros and cons.

Goals of this course

In this course I’ll assume that you have no prior knowledge of Node.js.

We’ll look at the document-based MongoDb for data access. Node.js can access traditional relational databases as well but Node is often backed up by NoSql databases like MongoDb or RavenDb. Note that I have a MongoDb tutorial on this blog starting here. If you’re not familiar with MongoDb at all then I recommend that you skim through the first two parts of that series. We won’t completely leave all our .NET tools behind as we’ll build our demo application with Visual Studio.

Note: you can build web sites hosted on Node. There are templating engines available to set up your views like you do in an ASP.NET MVC application with Razor. In this course, as the title suggests, I’ll first and foremost concentrate on building an API, not a website. In other words we’ll be looking into pure back-end web service coding, i.e. the equivalent of the Web API in .NET. The web framework behind HTTP APIs hosted on Node is called Express.

Note 2: I’ll build the demo with Visual Studio 2013 Professional. The Node.js development tools are at the time of writing this post not available for the Express edition.

Installation on Windows

Navigate to the Node.js homepage and click the green Install button in the middle of the screen. At the time of writing this demo the current version of the platform is v0.10.32. Node is a mature product but is still under heavy development so by the time you read this post there will almost certainly be an updated version.

You’ll get an msi package from the website. Step through the installer and accept the default values. You might want to change the installation folder as you wish but I’ll go with the proposed c:\program files\nodejs folder. Let the installation run complete.

Node.js command line interface

You can enter the Node command line interface (CLI) in several ways:

  • Open the Nodejs installation folder and double-click the application file called “node”
  • Open a command prompt, navigate to the installation folder and type “node”
  • If you just want to be able to type “node” in the command prompt then you’ll need to add the path to the node application file to the environment variable Path. The Node.js installation process will actually add it to the Path System variable, see screenshot below.

Environmental variable NodeJs home

If you open a command prompt and type “node” and press enter then the command may not be recognised. There are two ways to solve that:

  • Run the command prompt as an Administrator – it will then be able to access the System level variables
  • Open the Environment variables window again and add the path to node to the user-level PATH variable, see screenshot below

Add environment variable to user level settings

The Node.js CLI is not very dramatic, you should only see a “greater-than” sign:

Nodejs CLI start

You can write JavaScript commands in this CLI to interact with Node. We won’t do that, however. The point of this exercise was to make sure that the command “node” is available everywhere.

Setting up Node.js in Visual Studio

There are Node.js tools available for Visual Studio that can be downloaded on the project homepage here but they won’t work with the Express edition of Visual Studio. Currently the project is in Beta. Click the purple “download” button which will get you an msi installer. Run through the installer accepting all the defaults. Open Visual Studio when the installer has run complete. Under “Other languages, JavaScript” you should see some new template specific to Node.js:

Node.js templates in Visual Studio

These are “normal” templates, like ASP.NET MVC4 or C# Console App, that will create a VS project for you with the expected files, like .sln.

Select the blank console app template. This will give you an app.js file with one row:

console.log('Hello world');

As you may have guessed this is the JS equivalent of Console.Write in this context. Console.out will print a message to the available output window. You are free to write JS code in app.js and press Start. As this is a normal VS project you’ll get intellisense, breakpoints, step-by-step execution with F11 etc. Let’s test the following code:

console.log('Hello world');
var intOne = 4;
var intTwo = 10;
var result = intOne + intTwo;
console.log("...and the result is: " + result);

If you set a breakpoint at the last line and start the project with F5 then you should see an output similar to the following:

Hello world node js

You’ll see that it was in fact node.exe that hosted the console app.

There are two other files created by the template:

  • package.json: a metadata file holding some characteristics, like version and description, about the assembly in JSON format. We will look at this file in more detail in this series.
  • a standard read-me file that you see in GitHub projects

You’ll also see a tool called “npm” which is short for Node Package Manager. Don’t worry about it yet, we’ll be using it during the course. For the time being it’s enough to say that it is very similar to the NuGet dependency manager in .NET projects and Maven in Java projects.

In the next part we’ll start looking into MongoDb.

View all posts related to Node here.


About Andras Nemes
I'm a .NET/Java developer living and working in Stockholm, Sweden.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Elliot Balynn's Blog

A directory of wonderful thoughts

Software Engineering

Web development

Disparate Opinions

Various tidbits

chsakell's Blog


Once Upon a Camayoc

Bite-size insight on Cyber Security for the not too technical.

%d bloggers like this: