beanz Magazine

Node.js

Pedro Lozano on Flickr

Node.js is a software platform to create fast, scalable web applications quickly. These applications also use JavaScript on the server and web browsers.

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

Also In The March 2014 Issue

An Interview with Ben Heck

Ben Heckendorn is a self-taught maker with high energy, an internet show on Element14 (The Ben Heck Show), and a wonderfully wicked sense of humor. He also inspired this issue of the magazine.

Circuit Diagrams

Circuit diagrams are maps used to design and build electronic devices. This article describes connections, components, history, and tools used to create circuit diagrams.

Electronics Projects for Beginners

Here are links to projects, tutorials, parts, and online communities to help you get started with electronics projects for beginners.

Zina Lahr

Meet someone who used her love of electronics to create deeply fun and personal robots, gadgets, and other cool stuff.

Version Control Software

Version control software captures an evolving snapshot of one set of code along with all changes in case older versions need to be retrieved.

Brackets, Semicolons, or Nothing, Oh My

How do programming languages know what to process? And in the correct order?

JSON

JSON, or JavaScript Object Notation, is an elegant way to organize data into key:value pairs and make web applications work like software.

I do not think there is any thrill that can go through the human heart like that felt by the inventor as he sees some creation of the brain unfolding to success.

How to Use an Electronic Breadboard

Electronic breadboards are used to prototype electronic circuit designs and projects without messy solder or hot soldering irons.

Learn More Links for March 2014

Links from the bottom of all the March 2014 articles, collected in one place for you to print, share, or bookmark.

Node.js

Node.js is a software platform to create fast, scalable web applications quickly. These applications also use JavaScript on the server and web browsers.

Non-Relational Databases

These databases are relatively new technology to store data in databases. Here are a few options to explore.

News Wire Stories for March 2014

Interesting stories about computer science, software programming, and technology for the month of February 2014.