Node.js Development Part 2 – Visual Studio and Node.js

In this second part of my node.js development investigation we will look at using Visual Studio as a development environment.

To begin open Visual Studio and select New Project.

New Project
New Project

From the available project categories select Web and then from the template select ASP.NET Empty Web Application. Select a location for the project and then give it an appropriate name.

Create Empty Web Application
Create Empty Web Application

You should now be faced with an empty web project in the Visual Studio IDE.

Visual Studio Solution
Visual Studio Solution

Delete all default files/folders that were created with the project. Now create a JavaScript file called app.js with the following contents:

var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello World\n');
}).listen(1337, '127.0.0.1');
console.log('Server running at http://127.0.0.1:1337/');

This is the sample code from nodejs.org homepage that creates a basic webserver that will respond to http requests on port 1337 with the plain text Hello World.

App.js
App.js

Next the Visual Studio project needs to be configured to start up node.js when run so we can test the webserver just created.

Project Properties
Project Properties

From the top menu select DEBUG and Start Without Debugging or launch with Ctrl + F5

Run Project
Run Project

A command window should now open with the address of the running server.

Command Window
Command Window

Copy this address into a browser and voila the page is served by node.js.

Running In Browser
Running In Browser

You now have a running server that is responding to requests and serving responses over http. At this point there is little you can actually do with this, the next posts will cover adding packages to serve CSS files and HTML pages in a structured way, connecting to a database to serve content and running node.js under IIS stay tuned!

Node.js Development Part 1 – Installing Node.js

Node.js is a very fast application platform for building network applications, it is free to use for both personal and commercial applications. A simple webserver can be written in a few simple lines of code and there are currently over 30,000 Node Package Modules to extend the core functionality.

Applications are written in JavaScript which enables deployment across a large number of platforms. Installers/binaries are available for Windows, Mac, Linux and Sun OS with source also available for download. This series of posts will be focused on the Windows platform as this is where I do a majority of my work and will include advice on using Visual Studio 2012 as a development tool.

To install node.js simply browse to:

http://nodejs.org

and click the Install button in the centre of the page. This will download the Windows x64 msi installer.

After the installer has downloaded run the msi and accept the defaults as shown below.

Setup wizard
Setup wizard
End User License Agreement
End User License Agreement
Destination Folder
Destination Folder
Custom Setup
Custom Setup
Installation
Installation
Installing
Installing
Completed
Completed

And that’s it node should now be successfully installed, the next part will cover testing the installation by setting up a Visual Studio web project and creating a basic webserver in node.js.