Node.js on Flex Servers

Node.js is a platform for running server-side applications written in JavaScript. We offer Node.js in several versions for our Managed Flex Servers. This guide explains how to run a Node.js application on your Managed Flex Serv

Requirements

Node.js applications can be run with Managed Flex Server M and higher.

Building a Node.js application

For this example, we will write a Hello World web application. To make it accessible as a website over the internet, we will run it as the backend of an nginx reverse proxy.

We recommend creating an ~/app/ directory for your own applications and then creating a subdirectory here for each of your applications. This way, everything will always be neat and tidy. For our example application, we will create the directory ~/app/hello-world.

Now place your code here. Our example application needs a file called hello.js that will include the following content.

~/app/hello-world/hello.js
var http = require('http');
var server = http.createServer(function(req, res) {
  res.writeHead(200, {'Content-Type': 'text/plain'});
  res.end('Hello World\n');
});
server.listen(8080, '127.0.0.1');
console.log('Server running on port 8080');

As you can see, hello.js creates a listening socket and waits for HTTP requests. We select the IP address 127.0.0.1 because we do not want to put our application on the internet directly and instead use nginx as a proxy. The nginx proxy function assumes that your application can be accessed using this IP address. You can basically choose any port in  the range from 1024 to 65535. However, please note that only one listening socket per port can be created and starting your application would fail if the selected port is already assigned. So, it is a good idea to avoid ports normally used by other applications.

Tip: Please be aware that the term “localhost” refers to two addresses: the IPv4 loopback address 127.0.0.1 and the IPv6 loopback address ::1. Depending on how the “listen” directive is implemented by you or your application (that is, library or language), your application will listen to different interfaces accordingly. If you want to avoid this, enter the IP address directly.

Our example application does not require any additional NPM modules. If your application needs any of these, you can install them in the usual way using NPM. To do this, log into your hosting account via SSH, navigate to your application directory and install the desired module with the npm install command.

You can now start the example application with node hello.js. Press Ctrl+C to exit:

node hello.js
Server running on port 8080
^C

Integration

Because our example is a web application that would normally be accessible via http(s), you must configure your web server in the Control Panel so that it directs the requests to your new node application via proxy.

Web server configuration

In the Control Panel, create a website with the basic settings “Nginx proxy for own application”:

Nodejs EN

Server address and port
The proxy function expects that your application will be listening to the IPv4 loopback address 127.0.0.1. So here you only have to enter the port: 8080.

Using Custom Service Control

So far, you have been starting your application manually from the shell. While this an easy method for testing and development purposes, it is not an option for production operations. The Custom Service Control (CSC) can be used to run and manage your application automatically, e.g. starting and stopping it when performing maintenance on your server.

Configuring the hello.js app in the CSC is fairly simple. Set up a new custom service for the hello.js application:

hpservices supervisord add hello-world
successfully created dir for hello.js: /home/username/.services/supervisord/hello-world
successfully created dir for hello.js: /home/username/.services/supervisord/hello-world/log
successfully created dir for hello.js: /home/username/app/hello-world
supervisord service config file written: /home/username/.services/supervisord/hello-world/service.conf

A configuration framework is created in the file ~/.services/supervisord/hello-world/service.conf for the new service. All we have to change is the command for
our Hello World application. We can leave all of the other suggested values alone:

~/.services/supervisord/hello-world/service.conf
[program:hello-world]
command=/usr/local/bin/node %(ENV_HOME)s/app/hello-world/hello.js  ; the program (can take args)
directory=%(ENV_HOME)s/app/hello-world/     ; directory to cwd to before exec (default no cwd)
autostart=true                ; start application at supervisord start (default: true)
stopwaitsecs=10               ; max num secs to wait before SIGKILL (default 10)
stdout_logfile=%(ENV_HOME)s/.services/supervisord/hello-world/log/default.log
stdout_logfile_maxbytes=1MB   ; filesize at which to rotate logfiles (default ist 50MB)
stdout_logfile_backups=10     ; number of stdout logfile backups (0 means none, default 10)
stderr_logfile=%(ENV_HOME)s/.services/supervisord/hello-world/log/default.err
stderr_logfile_maxbytes=1MB   ; filesize at which to rotate logfiles (default is 50MB)
stderr_logfile_backups=10     ; number of stderr logfile backups (0 means none, default 10)

Using supervisorctl update, you can apply the new configuration for our Hello World
application:

supervisorctl update
hello-world: added process group

Attention: If you receive an error, you must uniquely execute before "hpservices supervisord start".

For more detailed information on working with the CSC and the options it offers, refer to the Custom Service Control guide.

Additional information

 

Unable to find what you were looking for?

Our support experts are happy to assist you personally!

 

© 2001 - Hostpoint AG