Understanding Node.js: The Technology Behind the Buzz

Node.js is one of the technologies that has received a lot of attention and understanding in recent years. But what is Node.js anyway?

In short, Node.js is a framework based on the currently fastest JavaScript compiler, Google V8. The focus is on highly efficient, asynchronous I/O, i.e. the connection of external resources such as the network or the file system. Since Node.js is based on JavaScript, the barrier to entry is correspondingly low, at least for web developers.

Write an http server

A common scenario is the development of server-side web applications. For this, Node.js contains the http module, which provides functions for developing web servers. To use this module, just import it using the require function:

var http = require(‘http’);

You can then call its functions, allen preceded by createServer, which creates a new web server. This function expects another function as a parameter, which accepts the incoming request and the outgoing response as objects. In order to actually run the web server, you then have to call the listen function with the desired port:

var http = require(‘http’);
var server = http.createServer(function (req, res) {
res.write(‘Hello Node.js!’);
res.end();
} );
server.listen(3000);

To start this first application, just call the following statement on the command line:

$ node app.js< br />
Since Node.js is platform-independent, it not only works on Unix-like operating systems such as OS X and Linux, but also on Windows. The developer is therefore free to choose a suitable platform for development.

In order to inform the web browser that the returned content is not pure text but an HTML file, you must also transfer the content-type header. The writeHead function is used for this purpose, which, like the write and end functions, is also available on the res object:

var http = require(‘http’);
var server = http.createServer(function (req, res) {
res.writeHead(200, {
‘Content-Type’: ‘text/html’
}) ;
res.write(‘Hello Node.js!’);
res.end();
});
server.listen(3000);

Use additional modules

Apart from the http module, Node.js contains a number of other modules, for example for implementing TCP/IP-based servers, accessing the file system or processing streams. A list of all included modules and their description can be found in the Node.js documentation./p>

In order to inform the web browser that the returned content is not pure text but an HTML file, you must also transfer the content-type header. The writeHead function is used for this purpose, which, like the write and end functions, is also available on the res object:

Node.js is supported by an exceptionally active community that has already installed numerous additional modules in recent years has developed. Several modules are now available for almost every application. The bandwidth ranges from the connection of databases to communication based on various protocols to video and audio programming.

This whole pool is managed with the help of npm (Node.js Package Manager). However, it is problematic to first gain an overview of the currently around 40,000 modules.

Unlike in the web browser, Node.js does not run the JavaScript code within a sandbox, giving you broad access to the underlying system. This allows Node.js to be used not only to develop web applications, but also as a replacement for Bash or PowerShell scripts, for example.

Strengths and Weaknesses

Its greatest strength is the ease of programming highly scalable applications in a language familiar to any web developer. However, Node.js can only develop its true potential if a lot of I/O access is actually carried out. Node.js is also particularly suitable for the implementation of push services, for example based on web sockets.

Another interesting aspect of Node.js arises from the use of JavaScript: Many REST-based web services today use JSON – a data format that JavaScript handles natively. Node.js is therefore particularly well suited to developing such services, especially in connection with a NoSQL database, which also speaks JSON.

Nevertheless, Node.js is not equally suitable for all scenarios: If an application is hardly based on I/O access, but rather on extensive, CPU-intensive calculations, Node.js is largely unsuitable. It is therefore important to weigh this up on a case-by-case basis.

Conclusion

Node.js is an extremely interesting technology that allows complex and very scalable web applications to be developed with just a few lines of code. The use of JavaScript enables web developers to get started easily and without any problems.

The challenge with Node.js is not to master the (relatively small) core, but to get to know the extensive ecosystem. This can only be done with time and patience – or appropriate guidance from a developer who is already experienced in Node.js.

Anyone involved in the development of I/O-heavy web applications, web-based push services or REST web services is well advised to take a look at Node.js and understand the buzz.

Mit Centron Colocation: Sicherheit und Effizienz für Ihre IT-Infrastruktur

Create a Free Account

Register now and get access to our Cloud Services.