Categories

Node.js installation and setup

29.04.2014
Author:

Node.js is an open source platform for writing server-side web applications. It is based on an event-oriented and asynchronous programming with non-blocking I/O. The platform is designed to execute standalone web applications in JavaScript. It internally uses the Google V8 JavaScript engine to execute code.

Node.js can be integrated with Drupal 7, using the module Node.js integration. Let's try it in action, and at the end we will see whether node.js works correctly with this module.

First, you should install all the necessary packages for work with node.js and node.js itself:

sudo apt-get install build-essential git curl openssl libssl-dev

The most common method of installing node.js is to compile it from the source codes.

Note: you should replace v.0.8.x with the latest stable version from https://github.com/nodejs/node-v0.x-archive

mkdir -p ~/local/src

cd ~/local/src

git clone --branch v0.8.x git://github.com/joyent/node.git

cd node

./configure

make

sudo make install

If there were no errors in the installation process, the node.js will be available for you. For example:

$ node

> console.log('Hello world');

Hello world

Our next step is to install the module node.js on Drupal. Then you should enter the directory with this module, using the following command:

$ cd path/to/your/nodejs/module/directory

Next the web developer should install all the required dependencies:

$ sudo npm install

$ sudo npm install socket.io

$ sudo npm install request

$ sudo npm install express

$ sudo npm install connect

After that you should enter node.js configuration page. You need to copy contents of "Suggested configuration" field, to create a sites/all/modules/nodejs/nodejs.config.js file and to paste the copied settings there.

Nodejs configuration

That is all, node.js platform is configured. To check whether it works you need to enable the "Nodejs Watchdog" module, which is included to the module’s Node.js integration package.

Not to type the same command a lot of times we can run ‘node server.js' in the background mode using forever package. Forever is able to manage the background processes: it can restart them after failure; redirect standard errors to the log files; and perform other useful functions. So you should install it on the server:

$ sudo npm install -g forever

Now you need to write 'forever start server.js' instead of 'node server.js'. To stop it you should execute 'forever stop server.js' function. You can also use 'forever list' to check what processes are running through forever.

To make node.js run automatically after system’s boot/reboot, you should create the init script in /etc/init.d/ directory.

In our case the script looks like this:

set -e
PATH=/usr/local/bin:/bin/usr:/bin:/sbin/:/usr/sbin
DAEMON=/var/www/node.loc/sites/all/modules/nodejs/server.js
case "$1" in
    start)
        forever start $DAEMON
        ;;
    stop)
        forever stop $DAEMON 
        ;;
    force-reload|restart)
      forever restart $DAEMON;;
    *) echo "Usage: /etc/init.d/node {start|stop|restart|force-reload}"
      exit 1
      ;;
esac
exit 0

You should save it in the file /etc/init.d/node and write the following commands in the terminal:

$ sudo update-rc.d node defaults 
$ sudo chmod +x /etc/init.d/node

That's it. Node.js setup is completed.

7 votes, Rating: 5

Read also

1

Often one can see such charges against Drupal: “It is impossible to build a high-load website using this framework”. We will refute this myth on...

2

One of the main issues after making decision to launch a website is development platform - CMS or custom coding? Let's elucidate Drupal framework advantages.

3

Very often a lot of developers face the issue of flexible materials sorting on the site. One of the solutions to solve this issue is Radioactivity module. Learn more about this tool.

4

We use "Git Flow" repository operations model quite often in our work. Let’s consider the scheme in detail.

5

Changes in Drupal 8 have also affected the process of creating your own widgets and formatters. The new Plugin API significantly simplifies this procedure.

Subscribe to our blog updates