Headless Drupal & Node.js | Part 2/3: Node.js

Open Source Drupal


  • Nederlands
  • English
Node Author

This is part two of the series “Headless Drupal & Node.js”, for part one, click here. In this blog I will give you a 'Hello Node' introduction. 

About Node.js

Node.js uses complex techniques and can therefore be confusing to work with. It is therefore not suitable for the novice web developer.

A key technology is that Node.js can perform Javascript on the server. It is using the V8 engine to do so, but then on a server (Google Chrome is also using this). So this way you can perform Javascript on a server as a replacement for f.e. PHP. Node.js adds a few unique features to it:

Event driven

Node.js is just like client-side Javascript: waiting for an ‘event’ and then reacting to it. It is an ‘asynchronous event driven framework’, inspired on techniques as Ruby's Event Machine or Python's Twisted. What is the benefit of that on a server? In this way Node.js can handle many events simultaneously: a request from a client, a database query or a file transfer.

Non-blocking

Node.js does this with ‘non-blocking’ techniques: it does not wait for a desired response, so it doesn’t block. This is different than a traditional server that is waiting for a request of a client, and until that request is fully handled, it will block that traditional server for all other requests. When a request generates a heavy database then the entire server will block, also for all other clients.

Two way connections

Node.js does not wait, it exceeds in processing ‘two way connections’: Node.js is for example not waiting for a response of the database after a query. But returns to action as soon as the database returns information.

Node.js's power

This makes Node.js great to use for real-time applications. Because of its non-blocking architecture, Node.js can handle many connections simultaneously. So it can be used for high performance server-client applications, for example a website with a huge amount of traffic with many logged in visitors. But is great to be used for:

  • Data pushing to a client.
  • Data exchange between clients, for example chats, multiplayer games & real-time data streaming.

Namedropping

Node.js is among others used by Walmart and Paypal.

Challenges Node.js

  • It is not possible to use traditional servers (f.e. Apache), so a good hoster will be more difficult to find.
  • Knowledge of Advanced Javascript is required.
  • The Node.js core is compact, you will need additional modules and customization for desired functions. You will need to learn what you can use best and how.
  • Node.js is primarily not designed for building websites, we therefore need additional libraries. In this case we have chosen for Express JS.
  • Quite a lot of development installs are needed: Node.js itself, Express JS, additional modules, Git.
  • When you have little experience on a new platform, you will not know the pitfalls. And every platform, including Node.js, has its pitfalls.

Introduction to implement Node.js

1. Locally installing Node.js

First check if it is already installed: enter 'node --version' in your terminal. If you get to see a version, then it is already installed:

If not, then visit https://nodejs.org, download and install it.

2. ‘Hello Node’

Usually these ‘hello world’ exercises are kind of lame, but for Node.js this exercise is immediately giving a proper introduction to the architecture and applied Javascripting.

2.1. Create a folder, for example on your desktop, ‘testnode’:

2.2. Add a file to that folder and give it for example the name ‘hellotest.js’.

2.3. Import modules

As indicated before, the Node.js core has a minimalistic setup. So in order to do something you will have to first import modules. This can be core modules or external modules (see also ‘npm’ further below).

First we need the ‘http’ module. Just like any other javascript library it will offer a number of methods and properties that can be used after the installation. You can import these as follows: add the following line to your file ‘hellotest.js’:

var http = require('http');

2.4 Create a server that gives a response

Then you can use the ‘createServer()’ method to create a server. You have to place it in a variable as follows:

var http = require('http'); // import module

var myServer = http.createServer(function(request, response) {
  response.writeHead(200, {"Content-Type" : "text/html"});
  response.write(" Hello Test Node");
  response.end();
}); 

myServer.listen(2000); // create server

We can place a custom function in createServer() that we define and work out ourselves, but we are using an ‘anonymous function’: a function without a name. This technique is also called a ‘callback’. When the function has finished its tasks, it will report the results to createServer(). Working with callbacks is a Javascript concept which is widely used in Node.js.

There are two parameters: ‘request’ & ‘response’. The server expects a request and will give a response with data to the client: a head (status code + type file) and the body (the html).

Finally we have to let the server listen to a particular port, in this case port 2000.

3. Implementing and testing

To test the application, open a terminal and browse to the folder ‘testnode’. Then enter ‘node hellotest.js’.

When you now open a browser and navigate to http://localhost:2000 you will get to see the response html.

When you apply changes to the response, you will have to restart the node server: interrupt the process in your terminal with ctr+c and restart it:

4. Modules - the Node Package Manager (NPM)

You can install and manage modules in Node.js via npm, you can find it at http://npmjs.org. Npm is also the command in your terminal, which allows you to manage modules. Just type ‘npm’. Instructions will then be shown in your terminal.

Package.json

Information (author, version, module dependency, etc) about your project will be managed in the package.json file. You can generate it by performing the command ‘npm init’. After a series of questions, the package.json file will be created:

Npm modules can be used stand-alone, but also as a dependency of your Node.js project. When you want to install for example Grunt, type: “npm install grunt --save”. You will now see that relevant module files are placed and that Grunt will be added as a dependency to your package.json:

End of part 2

You can see that it’s relatively easy to make a simple Node.js application if you are familiar with Javascript. But the Node.js core methods are very brief. Of course you do not want to build a fully dynamic website with just this kind of request/response functions.

In the next part 3, I will elaborate on Express JS: a web framework for Node.js with many built-in tools to build a website. I will also show you how to implement dynamic data from the RESTful Drupal API to Node.js / ExpressJS.

So, check out part 3 here!