mega888

An Introduction to Node.js

Administrator August 28, 2013

 

Node.js is a software platform that is used to build Scalable network (especially server-side) applications. Node.js utilizes JavaScript as its scripting language, and achieves high throughput via non-blocking I/O and a single-threaded event loop.

Node.js contains a built-in HTTP server library, making it possible to run a web server without the use of external software, such as Apache or Lighttpd, and allowing more control of how the web server works. Node.js enables web developers to create an entire web application in JavaScript, both server-side and client-side.

Node.js is a packaged compilation of Google’s V8 JavaScript engine, the libuv platform abstraction layer, and a core library, which is itself primarily written in JavaScript.

Node.js was created by Ryan Dahl starting in 2009, and its development and maintenance is sponsored by Joyent, his former employer.

Dahl’s original goal was to create web sites with as seen in web applications like Gmail. After trying solutions in several other programming languages he chose JavaScript because of the lack of an existing I/O API. This allowed him to define a convention of non-blocking, event-driven I/O

How to install node.js

Mac

If you’re using the excellent homebrew package manager, you can
install node with one command: brew install node.

Otherwise, follow the below steps:

  1. Install Xcode.
  2. Install git.
  3. Run the following commands:
git clone git://github.com/ry/node.git
cd node
./configure
make
sudo make install

You can check it worked with a simple Hello, World! example.

Ubuntu

  1. Install the dependencies:
    • sudo apt-get install g++ curl libssl-dev apache2-utils
    • sudo apt-get install git-core
  2. Run the following commands:
git clone git://github.com/ry/node.git
cd node
./configure
make
sudo make install

You can check it worked with a simple Hello, World! example.

Thanks to code-diesel for the Ubuntu dependencies.
Windows
Currently, you must use cygwin to install node. To do so,
follow these steps:

  1. Install cygwin.
  2. Use setup.exe in the cygwin folder to install the following
    packages: 

    • devel → openssl
    • devel → g++-gcc
    • devel → make
    • python → python
    • devel → git
  3. Open the cygwin command line with
    Start > Cygwin > Cygwin Bash Shell.
  4. Run the below commands to download and build node.
git clone git://github.com/ry/node.git
cd node
./configure
make
sudo make install

For more details, including information on troubleshooting, please
see the .

Hello Node.js!

Here’s a quick program to make sure everything is up and running
correctly:

var http =require('http');
http.createServer(function(req, res){
  res.writeHead(200,{'Content-Type':'text/plain'});
  res.end('Hello Node.js\n');}).listen(8124,"127.0.0.1");
console.log('Server running at http://127.0.0.1:8124/');

Run the code with the node command line utility:

> node hello_node.js
Server running at http://127.0.0.1:8124/

Now, if you navigate to http://127.0.0.1:8124/ in your browser,
you should see a nice message.

Advantages of Node js

  1. Web development in a dynamic language (JavaScript) on a VM that is incredibly fast (V8). It is much faster than Ruby, Python, or Perl.
  2. Ability to handle thousands of concurrent connections with minimal overhead on a single process.
  3. JavaScript is perfect for event loops with first class function objects and closures. People already know how to use it this way having used it in the browser to respond to user initiated events.
  4. A lot of people already know JavaScript, even people who do not claim to be programmers. It is arguably the most popular programming language.
  5. Using JavaScript on a web server as well as the browser reduces the impedance mismatch between the two programming environments which can communicate data structures via JSON that work the same on both sides of the equation. Duplicate form validation code can be shared between server and client, etc.

Let’s start working together on your amazing project.

Challenge us. We want to work with you to create the really cool stuff.