Building a web service with Node.js in Visual Studio Part 1: foundations
November 3, 2014 Leave a comment
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.
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
The Node.js CLI is not very dramatic, you should only see a “greater-than” sign:
Setting up Node.js 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:
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); console.log("Bye!");
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:
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.
- readme.md: 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.