SHARE
Facebook X Pinterest WhatsApp

An Intro to Node.js

Written By
thumbnail
Rob Gravelle
Rob Gravelle
Apr 25, 2012

Node.js is part runtime environment and part library for building network applications using server-side JavaScript. It uses Chrome’s JavaScript runtime engine to execute JS code directly without the need for the browser sandbox. You may recall an article that I wrote about Aptana’s server-side JavaScript for their Jaxer server. By all accounts, it looks like that technology is now dead in the water. Does node.js have what it takes to get server-side JavaScript off the ground? To answer that question, this article will explain how to use node.js. Afterwards, you can decide for yourself whether or not it’s a technology that you could see yourself embracing.

 

Advantages to Using Node.js

Node.js is supposed to be three things: easy to use, fast, and scalable. It’s easy in that a little code goes a long way and uses it’s own concurrency model that’s a lot simpler than traditional O/S threads. Much like client-side JS libraries, node.js does provide abstraction so that a lot of the boilerplate code is handled under the covers. That’s a great thing if you’re a novice to intermediate developer, not so good if you’re an expert and you want to get at some low level functionality. The scalability results from node.js’s better memory efficiency under high-loads and non-blocking execution model. Functions in node.js almost never perform I/O directly, so processes never block. That leaves the developer free from worrying about dead-locking processes and able to focus on the code at hand. That’s just another reason why beginner to intermediate programmers are able to use node.js to develop fast systems.

Here is a code snippet that illustrates how easy it is to write a simple server. It listens on port 8080 and responds to requests with a Hello message:

var http = require("http");
http.createServer(function(request, response) {  
  response.writeHead(200, {"Content-Type": "text/plain"});  
  response.write("Hello from the Node.js server!");  
  response.end();
}).listen(8080);
console.log('Server is listening to http://localhost/ on port 8080…');

I think that we can all agree that this is some pretty easy coding!

 

Running the Code

Let’s say that we wanted to run the above server. Here’s how to do it.

You can’t run anything until you go to Node.js and download the latest version. It’s quite easy to install as there is an installer for both Windows and Macintosh. There’s also a compressed tar/gzip source code file.

Once you’ve completed the installation, you can try it out by opening a console and typing:

node -v

That will display the version of node.js that you are running.

To run the server:

  1. Save the above code in a file called server.js.
  2. Navigate to the folder to which you saved the server file.
  3. Type “node server.js” at the command prompt:
    E:\HTML Goodies\Node.js>node server.js
    Server is listening to http://localhost/ on port 8080
  4. Open your favorite browser, enter “http://localhost:8080” in the address bar, and voila:

 

Modules in Node.js

As the size of our JavaScript libraries grow in size, it becomes necessary to organize them into different groups of similar functionality. In Java, for example, these are called Packages. Classes that need to reference a file in another package may do so using the import statement. Node.js implements the exact same feature, except that it organizes files by module instead of by package. Likewise, it utilizes the require() method to load the relevant file. In fact, we already used it in the first code example:

var http = require("http");

The same process works for our own modules. Here’s one that gets the path of the requested resource from the request object. It uses the url module to parse the request url for the pathname and returns it to the caller:

var url = require("url");

function getPath(request) {  
  var path = url.parse(request.url).pathname;    
  return path;
}
exports.getPath = getPath;

Unlike a lot of other languages and libraries, node.js requires us to export public methods to the exports object. The last line of the code is where that happens.

Back in the server.js file, we can now import our requestInfo module just like we did with the http one. The only difference is that we have to consider the path from the current script. The “./” means that it resides in the same folder.

I also changed the content type to html so that we can print lines in separate paragraphs. Having saved the requestInfo module to a local variable, we can call the getPath() function by appending a dot to the requestInfo module, followed by the function call:

var http        = require("http");
var requestInfo = require("./requestinfo");

http.createServer(function(request, response) {  
  response.writeHead(200, {"Content-Type": "text/html"});  
  response.write("

Hello from the Node.js server!

");
  var path = requestInfo.getPath(request);
  response.write("

Request for ” + path + ” received.

");
  response.end();
}).listen(8080);
console.log('Server is listening to http://localhost/ on port 8080…');

 

Conclusion

I have always been a fan of using the same language on both the client and server sides. As it stands, with the array of server-side technologies available, it can be a real chore to reach a decision on which to use. Not to imply that variety is a bad thing, but there are definitely advantages to working with a familiar language. For that reason, let’s hope that node.js catches on.

Recommended for you...

Best VR Game Development Platforms
Enrique Corrales
Jul 21, 2022
Best Online Courses to Learn HTML
Ronnie Payne
Jul 7, 2022
Working with HTML Images
Octavia Anghel
Jun 30, 2022
Web 3.0 and the Future Of Web Development
Rob Gravelle
Jun 23, 2022
HTML Goodies Logo

The original home of HTML tutorials. HTMLGoodies is a website dedicated to publishing tutorials that cover every aspect of being a web developer. We cover programming and web development tutorials on languages and technologies such as HTML, JavaScript, and CSS. In addition, our articles cover web frameworks like Angular and React.JS, as well as popular Content Management Systems (CMS) that include WordPress, Drupal, and Joomla. Website development platforms like Shopify, Squarespace, and Wix are also featured. Topics related to solid web design and Internet Marketing also find a home on HTMLGoodies, as we discuss UX/UI Design, Search Engine Optimization (SEO), and web dev best practices.

Property of TechnologyAdvice. © 2025 TechnologyAdvice. All Rights Reserved

Advertiser Disclosure: Some of the products that appear on this site are from companies from which TechnologyAdvice receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. TechnologyAdvice does not include all companies or all types of products available in the marketplace.