Select your language

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 Server.

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 Hostpoint Control Panel, create a website with the basic settings “Nginx custom mode (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.

If you want to modify the backend server address on an existing website or add a new one, you'll find the setting under “Websites” > “Edit” > “Locations” > “Proxy settings” > “Backend server”.

NodeJS Backend Server Adresse 01 en

NodeJS Backend Server Adresse 02 en

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.

For Node.js the command has to be adapted and some environment variables have to be added (remove semicolon at the beginning of the line)...:

~/.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

Please use this form only to provide feedback on the above guide.
For support requests please use this form instead.

 

 

© 2001 - Hostpoint AG
Cookie

Wir verwenden Cookies  🍪

We use Cookies  🍪

Nous utilisons des cookies  🍪

Utilizziamo dei cookie  🍪

Die digitalen Auftritte von Hostpoint (Website, Control Panel, Support Center etc.) verwenden Cookies. Diese werden dazu verwendet, um Daten über Besucherinteraktionen zu sammeln. Wenn Sie auf «Akzeptieren» klicken, stimmen Sie der Verwendung dieser Cookies für Werbezwecke, Website-Analyse und Support zu. Gewisse essenzielle Cookies sind jedoch für eine ordnungsgemässe Funktion dieser Seiten unerlässlich und können deshalb nicht deaktiviert werden. Auch ohne Ihre Zustimmung können gewisse Daten in anonymisierter Form für statistische Zwecke und zur Verbesserung unserer Websites verwendet werden. Bitte beachten Sie unsere Datenschutzerklärung.

Hostpoint's digital presences (website, Control Panel, Support Center, etc.) use cookies. These are used to collect data on visitor interactions. If you click “Accept”, you agree to the use of these cookies for advertising purposes, website analysis and support. However, certain cookies are essential for the proper functioning of these pages and therefore cannot be disabled. Even without your consent, certain data may be used in anonymized form for statistical purposes and to improve our websites. Please note our Privacy policy.

Le sites Web de Hostpoint (site Web, Control Panel, Centre d'assistance, etc.) utilisent des cookies. Ces cookies servent à collecter des données sur les interactions des visiteurs. En cliquant sur «Accepter», vous consentez à l’utilisation de ces cookies à des fins de publicité, d’analyse du site Web et d’assistance. Certains cookies essentiels sont cependant indispensables au bon fonctionnement de notre sites Web et ne peuvent donc pas être désactivés. Même sans votre consentement, certaines données peuvent être utilisées sous forme anonymisée à des fins statistiques et pour améliorer notre sites Web. Veuillez prendre connaissance de notre Déclaration de protection des données.

Le presenze digitali di Hostpoint (sito web, Pannello di controllo, Support Center, ecc.) utilizzano i cookie. Questi vengono utilizzati per raccogliere dati sulle interazioni dei visitatori. Facendo clic su «Accetta», acconsente all’utilizzo di questi cookie per scopi pubblicitari, di analisi del sito web e di supporto. Alcuni cookie essenziali sono tuttavia indispensabili per il corretto funzionamento di questi siti web e pertanto non possono essere disattivati. Anche senza il Suo consenso, determinati dati potrebbero essere utilizzati in forma anonima per fini statistici e per l’ottimizzazione dei nostri siti web. Si prega di tenere conto della nostra Dichiarazione per la pivacy.

Ablehnen
Decline
Refuser
Rifiuta
Akzeptieren
Accept
Accepter
Accetta