Node.js

Pedro Lozano on Flickr

Two of the biggest programming problems today, and the past decade, are how to handle concurrency and blocking. Concurrency is the ability for a computer system to handle multiple requests at the same time, including requests that must interact with each other. Blocking happens when a computer process has to halt to let an earlier process complete.

If web browsers are to provide many different applications which used to be installed on individual computers, handling multiple requests in real time without blocking is key. Massive online games also are impossible without solving these problems.

Node.js is one solution to this problem (for example, Tornado and Twisted also solve this problem for Python). It is a platform to allow easy programming of fast, scalable applications that run on networks concurrently with no blocking. Node.js was first released in May 2009, by Ryan Dahl, and is under active development. The platform includes a server which runs independently of Apache, nginx, and other traditional server software. And Node.js uses an event driven architecture where events trigger responses from the system.

What Makes Node.js Special?

As the .js part hints, Node.js uses JavaScript to provide an extensive set of code programmers can use to develop software applications. The code makes it easy, for example, to create a web server, log activity, connect to databases, and perform other common tasks. Programmers who know JavaScript find it easy to learn Node.js. And JavaScript is relatively easy to learn for programmers who want to get started.

Node.js also makes it easy to build plugins, frameworks, and other code used in different applications. Express is a framework, for example, that incorporates and builds on Concurrent, another Node.js framework. This flexibility can reduce software development time dramatically. All third-party code is handled with the Node Package Manager, or npm for short, which makes it easy to find, install, and manage any additions to Node.js.

How easy is it to use? Once you download the software to your computer from the Nodejs.org site, here is the code to create a web server:

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/');

This code is fairly easy to describe:

  • var http = require('http'); simply defines a variable called http then uses the variable as a handle to assign to it the properties of the http code base included with Node.js. The http code base includes functionality needed to create and run a web server.
  • http.createServer(function (req, res) { ... }) calls the http code base and its createServer code and gives it a function which includes a request (shortened to req) and response (shortened to res). Note the () and {} in lines 2 through 5 are matched open and closed to identify what pieces go where.
  • res.writeHead(200, {'Content-Type': 'text/plain'}); defines the response output from the server as message type 200 (which means data was sent without errors) and content type of text or plain data (no binary files, for example).
  • res.end('Hello World\n'); defines the end of the response, which is always what appears on the web page when you use a web browser to connect to this server. In this case, the server will send and the browser will display the message, Hello World. The \n bit simply tells the browser to also send a newline after the message.
  • .listen(1337, '127.0.0.1'); is the last part of the http.createServer function called above. This bit simply says the server should listen to port 1337 at the localhost address on your computer. If you don’t see the connection between lines 2 and 5, the wrapper is http.createServer(...).listen(1337, '127.0.0.1'); with function (req, res) {...} in the middle of the wrapper.
  • console.log('Server running at http://127.0.0.1:1337/'); displays the 'Server running ...' message in your terminal console to confirm the server has started and is available at the web address http://127.0.0.1:1337. The address localhost:1337 also works. To stop the server, press the Control and C keys.
  • Bonus Points: see in line 3 this code, {'Content-Type': 'text/plain'}? This is data represented in JSON format which uses key:value pairs separated with a colon. JSON is another technology used to deliver content concurrently across networks. In this case Content-Type is the key, text/plain the value.

Once this code is put into a file, for example, a file named server.js, the server is started with this command run from the $ prompt at the folder where the server.js file is located:

$ node server.js

While all programming languages have equally nifty amazing examples to demonstrate their language, this code example shows the main feature of Node.js: the language hides most of the technical complexity needed to make applications that run concurrently across networks. A web server with six lines of code is amazing.

The fact you can use JavaScript for both the back end and the front end web browser also means less complexity. While Node.js does not run in a web browser, the JavaScript used in Node.js is the same as JavaScript used in web browsers. Coders only need to know one programming language to create web-based applications.

How is Node.js Used?

The Node.js platform is used mostly for applications that need to scale to handle inputs from many people with code and data stored across multiple machines and, in some cases, multiple data centers. It’s use of JavaScript makes it work well in Microsoft environments as well as open source operating systems. Node.js also uses the JSON data format and easily connects to CouchDB, MongoDB, and other NoSQL databases which also are optimized for concurrency.

Learn More

Node.js

http://nodejs.org/
https://npmjs.org/
http://devopsangle.com/2013/04/01/the-birth-of-node-where-did-it-come-from-creator-ryan-dahl-shares-the-history/
http://en.wikipedia.org/wiki/Node.js

Node.js Support

https://groups.google.com/forum/#!forum/nodejs
https://groups.google.com/forum/#!forum/nodejs-dev
http://stackoverflow.com/questions/tagged/node.js

Understanding Express.js

http://evanhahn.com/understanding-express-js/

Concurrency

http://en.wikipedia.org/wiki/Concurrency_%28computer_science%29

Event Driven Architecture/Programming

http://en.wikipedia.org/wiki/Event_driven_programming
http://en.wikipedia.org/wiki/Event_driven_programming

Visited 477 times, 1 visit(s) today