translated by Google

Machine-translated page for increased accessibility for English questioners.

Instructions for using Node.js on FI

Runtime environment Node.js used to create JavaScript programs. This platform is primarily intended for running stand-alone servers listening on a dedicated TCP port, which also applies to web frameworks. However, there are modules that allow you to run under the Apache web server and therefore also on Aise at . After reading this text, you should have enough information to choose the right solution for you.

We recommend that you read the instructions before proceeding Custom HTML pages , Creating CGI scripts , a Instructions for using PHP on FI . We assume that Apache has access to the directory public_html .


CGI is a simple interface that can be used in any programming language. In this manual we will use the module cgi-node which provides basic features for easier work with HTTP requests and responses.

First, we will create directories for our new application and support programs:

mkdir --parents ~/public_html/node-app/cgi-bin

Let's download the library cgi-node and set it the right to run:

cd ~/public_html/node-app/cgi-bin
wget -O cgi-node.cgi
chmod u+x cgi-node.cgi

Then in it we change the path to the Node.js binary on the first line to /usr/bin/node and set the path to the directory with the session files (variable SessionPath ) on /tmp :

sed -i "1s|.*|#!/usr/bin/node|" cgi-node.cgi
sed -i "s|SessionPath:.*|SessionPath: '/tmp/'|" cgi-node.cgi

We save the application itself to a file index.js . We will only call a function in it CgiNodeInfo() , which is suitable for testing and lists information about the environment, HTTP query and session:

cd ~/public_html/node-app
echo "CgiNodeInfo();" > index.js

You would write a simple "Hello world" application as follows:

response.headers['Content-Type'] = 'text/plain; charset=utf-8';
write("Hello world!\n");

More details on use node-cgi you will find in documentation or in source code cgi-node.js .

Finally, we tell the Apache web server that it has all the files with the extension .js run with cgi-node.cgi . Replace here and in the following text xlogin own login.

cd ~/public_html/node-app
cat > .htaccess <<'EOF'
AddHandler cgi-node .js
Action cgi-node /~xlogin/node-app/cgi-bin/cgi-node.cgi
DirectoryIndex index.js

Verify the functionality of the application by visiting .

You can also use other modules in the application. Either enter the full path to them ( /home/xlogin/... ) or install them in a directory ~/public_html/node-app/cgi-bin because they are looking relative to location cgi-node.cgi :

cd ~/public_html/node-app/cgi-bin
npm install wonderful-module


When using CGI, you will soon encounter the limits of this technology. Each HTTP request is processed by a newly started process, which in the case of Node.js means a relatively high latency. In addition, you cannot use any of the many frameworks that need a module to function http .

Fortunately, there is a module node-fastcgi which is built on specifications FastCGI . This allows the web server to let a long-running process handle multiple HTTP requests in a row.

First, prepare a directory with the application in which to install the module node-fastcgi :

mkdir --parents ~/public_html/node-fastcgi-app
cd ~/public_html/node-fastcgi-app
npm install node-fastcgi

The application itself index.js may look like this (taken from documentation ):


var fcgi = require('node-fastcgi');

var server = fcgi.createServer(function(req, res) {
    if (req.method === 'GET') {
        res.writeHead(200, { 'Content-Type': 'text/plain' });
        res.end("Hello, world!\n");
    } else if (req.method === 'POST') {
        res.writeHead(200, { 'Content-Type': 'text/plain' });
        var body = "";

        req.on('data', function(data) { body += data.toString(); });
        req.on('end', function() {
            res.end("Received data:\n" + body);
    } else {

The script must be given execution rights ( x ) for the owner:

chmod u+x index.js

Finally, we tell the Apache web server that it has .js to run files using FastCGI:

cat > .htaccess <<'EOF'
AddHandler fastcgi-script .js
AddType application/x-httpd-javascript .js
DirectoryIndex index.js

You can test its functionality at .


You can use the FastCGI server as a replacement for the module http . An example is the use with the framework Express . We will install this framework first (notes on the location of modules from the CGI section apply - they are searched relative to the script):

cd ~/public_html/node-fastcgi-app
npm install express

The application can then look like this:


var fcgi = require('node-fastcgi');
var express = require('express');
var app = express();

app.get('/~xlogin/node-express-app/', function (req, res) {
    res.set('Content-Type', 'text/plain');
    res.send("Hello World!\n");


and will be available at .

Nice URL

In order for you and the users of your application to work well with the URL, we recommend setting it in a file .htaccess rewrite URL using mod_rewrite :

cat >> .htaccess <<'EOF'
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} !^/~xlogin/node-express-app/index.js
RewriteRule (.*) /~xlogin/node-express-app/index.js/$1 [L,PT]

Here is the line with RewriteRule ensures that the URL shape will be serviced by an application stored in index.js . Line s RewriteCond prevents an endless rewriting cycle.

For example, you can connect your application using use under the given prefix and use paths independent of the specific location of the application:


var prefix = '/~xlogin/node-express-app';
var fcgi = require('node-fastcgi');
var express = require('express');
var base_app = express();
var app = express();

app.get('/hello', function (req, res) {
    res.set('Content-Type', 'text/plain');

app.get('/world', function (req, res) {
    res.set('Content-Type', 'text/plain');

base_app.use(prefix, app);


You can try addresses and .

© The text on CGI is based on the instructions of Patrik Vala.