Getting started with Node.js

Let’s suppose you’re a seasoned developer which primary language is Java, C#, Python, Ruby, PHP, C/C++, etc… Or even you’re beginning your carrier as a software developer. This post is a great start on node.js development path.

Before getting the hands on code with node, it’s really important to understand some basic concepts:

  • Node.js is a cross-platform javascript runtime (based on Google V8) for server-side web applications.
  • Event-driven architecture: node differs from the old web stack (resources overhead on memory usage per request). The node process runs in a single thread, it constantly consumes requests from event-loop that are calls to services or functions.
  • Non-blocking I/O API: node is fast due to this feature and event-driven model, these APIs contains callbacks which response will be handled later in another request pushed in event-loop. It makes your application asynchronous, scalable and light-weight. Keep in mind that intensive computation tasks (greedy loops) per request is a bottleneck and you must optimize it in chunks.
  • Built-in APIs: all libraries are distributed as modules, the most common one is ‘http’ used to run as a stand-alone web server.

 

Download and install node.js

Go to node.js download page and install it:

Hello world example with node shell

Go to your preferred prompt/shell command and type node as follow:

$ node
> console.log('Hello world!');
Hello world!
undefined

 

Notice that it displayed the console’s output and returned nothing (undefined). Let’s explore javascript in shell:

> 'abcdef0123xyz'.match(/[0-9]+/);
[ '0123', index: 6, input: 'abcdef0123xyz' ]

 

In case you aren’t familiar with the above example I’d recommend you get in touch with some commons javascript methods and concepts. It’s required for the next steps in this starting guide.

Your first web application in node

Code the following js file in your current project/shell directory:

var http = require('http');
var server = http.createServer(function(req, res) {
   var hello = 'Hello world!';
   res.writeHeader(200, {
      'Content-Length': hello.length,
      'Content-Type': 'text/html'
   });
   res.end(hello);
});
server.listen(8080);

At first glance we can see the callbacks and event-driven model in action, the http.createServer methods requires one callback to execute on every http request. This is a simple web application example to show you some concepts explained above.

Run your web application:

$ node server.js

Test your web application:

 

Next steps to victory

  • Read node.js related books, there are plenty of great books that will give you an overview about all features available and good practices during coding. Some of them also show you the most important javascript methods and concepts. It might save you a lot of time before struggling on common mistakes due to node’s event-driven architecture.
  • Understand node.js engine and built-in modules commonly used for daily basis, again, asynchronous web development are quite different than old stack development.
  • After you’re done with the two items above you’ll be able to explore node.js frameworks with no problems as Express, Socket.io, Meteor, etc..
  • Have fun with node.js, always! 😉
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s