I am aspired to learn Node and I had tough time setting up environment for Node development. Guess what I got none other than Mr Glenn Block to help me. We did pair programming over Google Hangout and he helped me setting up. It was his kindness that he spent time with me. He has been inspiring many like me and helping developers around. He has done great work on Node and you can read few of his learning material at below link,
https://github.com/glennblock/node-azure-workshop/blob/master/part1_basics_01.md
This blog post is dedicated to Glenn. Language of this blog post is in the way that I am talking to you however this blog is just text version of Glenn work done with me while doing pair programming.
Yes I know you are excited for Node.JS. You know Node runs your script at server rather at the client browser. Cool, now let us see how we can set up development environment for Node development. You have choice to use any text editor of your choice to work with Node. I am choosing Sublime Text 2 as editor to write code. So put your sleeves on and follow following steps to set up environment.
Step 1: Download and Install in Node.JS
Navigate to http://nodejs.org/ and click on download.
You will be navigated to download page. From here select required installer, download that and then install that. I am working on Windows machine so I selected option of Windows Installer.
After successful installation go to shell and verify successful installation. To verify it type npm on command prompt and you should get output as given below. It also verify that node is successfully added to the path.
There is another option to install node as well. You can install it from chocolatey.
http://www.chocolatey.org/packages/nodejs.install
Step2: Install Sublime Text
As we discussed in beginning of this post, you can work with any text editor of your choice to work with Node. We selected Sublime Text. You can grab sublime from here
After downloading and installing navigate to https://sublime.wbond.net/installation#st2 . From here you need to install package in Sublime Text. Choose package for installed sublime text version. In my case it is Sublime Text2. Copy text for package from here.
Now launch Sublime Text and open console. To open console you have two options
- View-> Show Console
- Type shortcut ctrl + `
On console paste copied text from previous step and press Enter. After package got installed we need to enable node package on Sublime Text. To enable this go to Tools-> Command Palette(Ctrl+ Shift+P) and search Node and install.
Once this is done you need to create working folder. You can create working folder anywhere. I have created a folder named NodeCodes in C drive. After creating folder we need to add folder to project. To do this navigate to Project -> Add Folder to Project. I have added NodeCodes folder as project.
Now you are good to go and environment for Node development has been setup at your machine.
Write Hello World and Run it
Now go ahead and create a file named Server.js in your working folder. In my case it is NodeCodes folder. In Server.js write below code. In further posts we will go into detail of these codes. As of now just copy paste them to run your first Node program.
Server.js
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/');
Now to run this go to command prompt and type Node Server.js . If you are not in working folder then do not forget to pass full path of Server.js
As output you should get server running message.
Once you get this output means your development environment is up and you are good to play around Node. In further post I will discuss other topics of Node and try to simplify Node learning. Once again Thanks to Glenn for his support.
Leave a Reply