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!