Originally published at: http://www.sitepoint.com/build-node-js-powered-chatroom-web-app-getting-started/
This article is part of a web dev tech series from Microsoft. Thank you for supporting the partners who make SitePoint possible.
This Node.js tutorial series will help you build a Node.js-powered real-time chatroom web app fully deployed in the cloud. In this series, you will learn how to setup Node.js on your Windows machine (or just learn the concepts if you’re on Mac), how to develop a web frontend with Express, how to deploy a node express apps to Azure, how to use Socket.IO to add a real-time layer, and how to deploy it all together.
Part 1 – Introduction to Node.js
Welcome to Part 1 of the hands-on Node.js tutorial series. In this instalment, I will explain what Node.js is, why you should pay attention to Node.js, and how to set up your machine.
What is Node.js? Why use it?
Node.js plays a critical role in the advancement of WebSockets as a method for real-time communication between the front and back ends. The use of WebSockets and the libraries building on that protocol such as Socket.IO have really pushed what is expected of web applications and lets us developers explore new ways to create the web.
How to Set Up Node.js on Windows 8
To get started, you’ll need a reasonably up-to-date machine. I will be showing you how to install Node.js on Windows 8.1.
1. Install Node.js
First, download and install the Node.js runtime. Choosing the Windows Installer is one of the easiest ways to get started.
Alternatively, if you are a fan of Chocolatey, the package manager for Windows, you can install Node.js by running:
choco install nodejs.install
2. Confirm Set-up
You should double-check that the Node.js executable has been added to your PATH system environment variable. Watch this video if you want to see how to change your environment variables on Windows 8 and Windows 8.1. You will want to make sure the following folder has been added to the PATH variable:
C:\Program Files (x86)\nodejs\
If you go to your Command Prompt and type in node –h, you should see the help documentation for the node.js executable displayed.
Along with Node.js, npm, the system used to manage node.js packages, should be installed and available on the Command Prompt as well. Simply type in
npm –h, you should see the help documentation for npm displayed.
In case of an error…
If you encounter an error similar to this one…
Error: ENOENT, stat 'C:\Users\someuser\AppData\Roaming\npm'
…the resolution is to create a folder at the path specified above, as shown in this StackOverflow question. This is only an issue in the latest Node.js installer and should be resolved by next release. You can create the folder like so:
mkdir -r C:\Users\someuser\AppData\Roaming\npm
Select a Development Tool
With Node.js installed, it’s time to select a development tool. Feel free to use any editing tool you want. Throughout these tutorials, I will use Visual Studio to develop, debug, and deploy the chat engine; as well as Node.js Tools for Visual Studio (NTVS) –a free open-source plug-in for Visual Studio that supports developing Node.js applications.
What’s cool about NTVS is that it adds Node.js support for editing, Intellisense, performance profiling, npm, TypeScript, debugging locally and remotely (including on Windows/MacOS/Linux). It also makes it easy to quickly create web sites and deploy them to Microsoft Azure Web Sites or Cloud Services.
If you do not have Visual Studio, you can download a free copy of Visual Studio 2013 Community Edition. Then, download the free Node.js Tools for Visual Studio plug-in. You may also use Visual Studio 2012 Pro or higher (requires Update 4) or Visual Studio 2013 or higher (requires Update 2). The NTVS plug-in is compatible with both editions.
Start a new Node.js project in Visual Studio
Note: Screenshots show Visual Studio 2013 Ultimate.
Starting a new Node.js project is fairly straightforward.
- Start Visual Studio. On the File menu, click New, and then click Project.
- You will be presented with the following screen. Feel free to explore Visual Studio at this point. You will want to open the generated
server.jsfile in the Solution Explorer (on the right typically but may be located elsewhere on your screen.)
- You can now debug your node.js web application in your preferred browser.
As is typical in other languages, the generated code example shows you how to output “Hello World” in the browser. Let me explain how the generated code in