| By Hovhannes Avoyan | Article Rating: |
|
| July 2, 2011 03:46 PM EDT | Reads: |
635 |
There is an almost insurmountable myriad of server-side environments and web servers all aiming to accomplish the same task: doing the most work in the most efficient way in the least amount of time. However, there are few of these technologies which arguably conquer the many road blocks to achieving this goal (namely concurrency issues and handling the overhead of connection allocation). Among these select few is Node.js.
Node.js, in its purest form, is a server-side environment built on top of Google’s V8 Javascript Engine. It is geared primarily toward the facilitation of network and server programming, keeping both ease of use, speed, and capability in mind. In fact, the Node.js website demonstrates just how simply you can get up and running with a respectable HTTP server “Hello, World” example:
var http = require('http');
http.createServer(function (request, response) {
response.writeHead(200, {'Content-Type': 'text/plain'});
response.end('Hello World\n');
}).listen(8080);
console.log('Server up and running!');
But do not be too quick to question Node’s aptitude based off of its simplicity, for it has some clever tricks up its sleeves.
One of the most crucial and fundamental features of Node.js is its broadened use of callback functions. Node uses this feature to conquer our first road block of concurrency issues, seamlessly eliminating many common frustrations involved in the writing of asynchronous code. To do this, all of Node’s built-in modules for things such as I/O and socket handling is implicitly asynchronous and uses the callback paradigm, triggering callbacks as end events.
Node also conquers our second road block of reducing the overhead of allocating memory for multiple threads. In fact, the beauty of it all—and most surprisingly—is that Node.js runs everything from a single-threaded event loop which comes with some great advantages over classical multi-threaded designs.
Getting up & running
Node.js is by far one of the quickest server-side environments to get up and running. The following instructions are brevities of the steps described here:
1) Go to the Node.js website and follow the links to the download.
2) If you’re running on Windows, you will need a shell tool such as Cygwin. You will also need to get a compiler, such as gcc or Xcode (for Macs) if you plan on building Node from source (there are some pre-built binary distros for Windows machines here and via popular package managers for Mac or Linux machines here).
3) From the command line, use the following commands to build and install Node:
git clone --depth 1 https://github.com/joyent/node.git
cd node
mkdir ~/local # change to desired installation path
./configure –prefix=$HOME/local/node
make
make install
export PATH=$HOME/local/node/bin:$PATH
After that, you’re done! Create a new Javascript file named something like “server.js” using the Hello, World code as an example script, then execute node /path/to/server.js , and you’ve now successfully started a simple Node.js HTTP server which you can access in your browser at http://localhost:8080 (or wherever you are running the server from).
The Node.js module system
Node uses a fairly useful module-based system to provide “include”-esque functionality most comparable to CommonJS’s implementation. In the Hello, World code, var http = require(‘http’); is used to load the “http” module and store the reference to this module in our variable aptly named http (using the same name for the variable as the module is not required of course). The syntax is the same for all of the Node.js core modules (net, fs, sys, etc).
Node also allows for writing custom modules using a similar syntax (though with a different inclusion path) such that var my_module = require(‘./my_module’); will load and include the “./my_module.js“ file. Specifics on building custom modules will be discussed in a later chapter.
Share Now:











Read the original blog entry...
Published July 2, 2011 Reads 635
Copyright © 2011 SYS-CON Media, Inc. — All Rights Reserved.
Syndicated stories and blog feeds, all rights reserved by the author.
More Stories By Hovhannes Avoyan
Hovhannes Avoyan is the CEO of Monitis, Inc., a provider of on-demand systems management and monitoring software to 50,000 users spanning small businesses and Fortune 500 companies.
Prior to Monitis, he served as General Manager and Director of Development at prominent web portal Lycos Europe, where he grew the Lycos Armenia group from 30 people to over 200, making it the company's largest development center. Prior to Lycos, Avoyan was VP of Technology at Brience, Inc. (based in San Francisco and acquired by Syniverse), which delivered mobile internet content solutions to companies like Cisco, Ingram Micro, Washington Mutual, Wyndham Hotels , T-Mobile , and CNN. Prior to that, he served as the founder and CEO of CEDIT ltd., which was acquired by Brience. A 24 year veteran of the software industry, he also runs Sourcio cjsc, an IT consulting company and startup incubator specializing in web 2.0 products and open-source technologies.
Hovhannes is a senior lecturer at the American Univeristy of Armenia and has been a visiting lecturer at San Francisco State University. He is a graduate of Bertelsmann University.
- Ubuntu-based Open Source Linux Mint Tests KDE Version
- Eucalyptus Loses Ubuntu to OpenStack
- Ubuntu Cloud to Transition to OpenStack
- Adobe AIR Dead on Desktop Linux
- Red Hat Wheels Out MRG 2.0
- Nuxeo Releases New Version of Open Source Document Management Software
- Hyper-V Now Supports CentOS
- Canto Cumulus 8.5 Introduces World Metadata, Improved Workflow and Easier Administration
- Create Linux User Login Monitor on Monitis
- Oracle Releases VM Template for MySQL Enterprise Edition
- Twenty-Five Linux Server Hardening Tips
- NComputing enthüllt vertikale Marktlösung, um Desktopvirtualisierung in Europa voranzutreiben
- My Two-Year-Old Daughter Sofia Facing Life-Long Disability in Syria
- Cloud Expo Silicon Valley Opens Call for Papers for “Cloud Foundry” Track
- My Abducted Two-Year-Old American Guest Worker Daughter in Syria
- State Department Photos of My Abducted Daughter Sofia in Syria
- Uncle Who Kidnapped Sofia to Syria Still a Practicing MD in Chicago
- Centrify Goes Cloud
- Ubuntu-based Open Source Linux Mint Tests KDE Version
- Eucalyptus Loses Ubuntu to OpenStack
- Cloudera Puts Out New Hadoop
- Texas Jury Duns Google $5 Million in Linux Case
- Feras Bankosly, Uncle Who Kidnapped Sofia to Syria, Is MD in Chicago
- Adobe Falls Out of Love with Linux
- The i-Technology Right Stuff
- Linux.SYS-CON.com Exclusive: Linus Discloses *Real* Fathers of Linux
- After Ubuntu, Windows Looks Increasingly Bad, Increasingly Archaic, Increasingly Unfriendly
- Linus' Top Ten SCO Barbs
- A Closer Look at Damn Small Linux
- SCO CEO Posts Open Letter to the Open Source Community
- Netscape Co-Founder's 12 Reasons for Growth of Open Source
- Where Are RIA Technologies Headed in 2008?
- *POINT - COUNTERPOINT SPECIAL* What's Wrong with the Open Source Community?
- Introducing "Cooperative Linux" - Linux for Windows, No Less
- Linux.SYS-CON.com Exclusive: What Would UserLinux Look Like?
- Why Recovering a Deleted Ext3 File Is Difficult . . .

































