NodeJS and Why I Like It?

I’m sure by now if you come across any web developer magazine, you will frequently hear the buzz word about NodeJS and how useful / powerful it is to web/API developers etc and such.

But, most importantly why on earth do we need nodeJS anyway?

Today, I’m just gonna blog about nodeJS from my perspective and how useful it is to my daily job as a developer. Ok, first off my definition of nodejs in pointer form below :-

  • Lightweight and easy to setup
  • Works in all platforms
  • Multipurpose server, depending on which and what module you install.
  • Has this cool feature called Node Package Manager or npm, which is an online repository of all node related projects and modules to install for free.
  • Run entirely using Javascript – NIIIIIIINNNNNNJAAAA!!!! Sorry cant help myself.

 

Why I used nodeJS for?

  • On the fly webserver. – No configuration files, just source code and command prompt parameters.
  • Developed a fast simple test API calls that returns some values for mobile development. Dont even need to write a class or compile, just return a variable in JSON.
  • Used it to setup weinre, a cool remote debugging tool. It doesn’t even need any coding for this, my goodness.
  • You start the server by command prompt of no more than 3 words. Stopping it just matter of press Ctrl+C. Don’t even need to restart your mac/pc other than first time setting up the runtime.
  • Because I’m a JS NINJA! – nuff said

 

What’s the basic requirement in picking up nodeJS?

  • Some knowledge on how command/console prompt works (mac/wins/linux).
  • Setting up nodeJS runtime, all you need is to make sure node command in everywhere. (may require some environment variables setup like Java)
  • Javascript jutsu! You must already know your favourite stacks if you want to use nodeJS for development. A stack is a combination of javascript framework that work together. Stack is like the concealed weapons and shuriken packs a ninja has to choose for his/her arsenal. My favourite stack for NodeJS is Express + Embedded Javascript (EJS templating) and sometimes, might combo with JQM.
npm Install FTW!

 

Ok, I got all that now what?

  1. Install the node runtime, using installer of your platform here http://www.nodejs.org
  2. Once installation is done, restart machine. Check to make sure the node runtime runs everywhere.  All it takes, is just to run the command prompt or console. type in command node, if you do it right you will see the runtime console appearing like > below it, just use Ctrl+C twice to exit.
  3. Ermmm thats it?  Yeah that’s it.
Ermmm...Ja thats it 🙂

I want a simple webserver to display basic html5 pages with no installation hassle or any big configuration. How to proceed?

  • Fastest way, without coding much is to just use connect module.
  • goto Command prompt, you should have a central working nodeJS folder created to host all your server files. I suggest creating nodejs folder to contain all your server projects.
  • Each server is known as an app. Within nodeJS folder create <yourappname> folder. We are going to install connect module here.
  • Where and how to download connect module? change directory in command prompt to <yourappname> folder you just created. Type in this command “npm install connect” and for node to pull module from repository to your folder
  • All you need now is to create a folder to store your web files. Create a www folder. Copy and Paste your files here.
  • To write a html server using connect, all you need is this few lines below. Save it into some js file called server.js. Then just, fire up the server, “node server“. A server app you can copy to flashdrive and even execute from there, provided the host has node installed.
  • As you can see no hassle of setting up config files, no htdocs to select, everything just runs within the app folder. You might wanna change your port number from 8080 to another if you like..but seriously no waiting for compile, no worries…whenever you change the server.js remember to restart it, by quitting it and repeat previous point above.
var connect = require('connect');
connect.createServer(
    connect.static(__dirname+'/www')
).listen(8080);
console.log('Your Server Started...Check http://localhost:8080, make sure copy paste your html files into www...Ctrl+C to quit');
 
//Start your server using node command "node server" launching in the same folder.

How do I know what modules I can install for nodeJS?

Go here https://npmjs.org/


Ok, I’m ready for more like remote API calls?

For that, I prefer to use express module to write these, again installing a module is as simple as create an app folder for the server, then “npm install <module name>“. Unfortunately, I’m not blogging on that now due to time constraints (I have LOTS of research and other stuff ongoing) so I’ll stop here for now. Stay tuned for Express API tutorial for NodeJS article from me soon.

www folder and server file

 

Comments

comments

Leave a Reply

Your email address will not be published. Required fields are marked *