2.10. General information about Node.js
For business hosting only
Node.js is only available on businesshosting and is included in the price of the tariff. At change of tariff on shared hosting sites on Node.js will stop working.How Node.js works on hosting
Node.js works on hosting just like it does on VPS or dedicated server, but with HTTP traffic proxying in a Node.js application.
Proxying HTTP traffic is necessary to be able to protect your application from DDoS attacks and provide you with additional features of our hosting, such as installing free SSL certificate from Let's Encrypt, caching static files etc. Without proxying, HTTP traffic will not reach your application. If you don't need HTTP traffic, you can run the application without it — everything will work.
There are two types of proxy available:
- By socket. When the application starts, a socket file is created into which all HTTP traffic is proxied. The advantage of this approach is that the socket interface itself works faster than proxying by IP address, the disadvantage is that not all popular frameworks can run an HTTP server over a socket and only accept parameters in the settings
--hostname
and--port
. If you have the ability to run an HTTP server on a socket — we advise you to do just that, if this is not possible — use proxying by IP address. - By IP address. Hosted HTTP traffic can be proxied through a local IP address. It is organized as follows. We give you a unique local IP address of the form
127.*.*.*
, where*
— any number from 0 to 255, and the default port —3000
. You specify these values in the settings of your application. Depending on your framework, these parameters may be specified in different ways, but in most cases they are application launch parameters.--hostname
and--port
. Note Instead of a local IP address, you can use any of the ordered dedicated IP - in this case, it is allowed to borrow any port. To use a dedicated IP, you must explicitly specify it as the second parameter inserver.listen(port, ip)
.
Proxy type is selected in site settings when including Node.js. Data for using the selected type is displayed on the page Node.js settings. They are also contained in environment variables: process.env.PORT
and process.env.HOSTNAME
(respectively, the path to the socket and an empty value when proxying by socket, or the port and IP address when proxying by IP address).
Request processing order:
- The request goes to the public server on port 80 or 443.
- Based on the host name, it is redirected to the application in accordance with the selected type of proxy — to the socket of the desired site or a local IP address.
- The application receives the data, processes it, and returns a response.
Application launch order
- Openup site settings.
- Select Node.js web server, desired version, type of HTTP traffic proxy and save the changes:
- Place your application files in root directory of the site - download the files of the finished project or install using npm.
- Add a script with the command
start
inpackage.json
- Click «Restart» in Node.js settings.
Possible problems:
- When starting the HTTP server not with our socket - the application will continue to work, but the site will have a 502 error.
- When starting the HTTP server not with our IP address and port - the application will exit in a couple of seconds with an error code
9
. - When starting the HTTP server with port only, no host specified - the application will occupy the port globally and will interfere with the launch of applications on other hosts within the hosting account. The solution is to specify the host in
app.listen()
. - Error 502 generally means that we cannot find where to proxy your traffic. This usually happens when the application is started with the wrong IP address, port, or socket (see above), or when the application fails to start.
- When the application crashes due to some internal error, our Supervisor system will detect this and try to restart the application within 10 seconds. It will keep trying until your application is up and running.
If you need to run an application from console and you are using proxy by IP address, then at startup you explicitly specify the IP address and port:
HOSTNAME=127.*.*.* PORT=3000 node index.js
Instead 127.*.*.*
and 3000
substitute the IP address and port from the page Node.js settings.
General information
Attention!
- npm is installed in the hosting account after installing Node.js as a web server or changing its version in site settings.
- For correct operation of Node.js to the file
.bashrc
lines with NVM configuration are automatically added. - If node and npm are not visible in the console — make sure that the files exist in the hosting account
.bashrc
and.bash_profile
and they are error free. - nvm allows you to switch to any version of Node.js that was previously selected in site settings.
npm
npm is the Node.js package manager. Allows you to install additional packages directly on the hosting. Enough to use connect via SSH and execute the necessary commands in the site directory.
Commands:
npm -v
— view the current version of npm.npm install package
(ornpm i package
) — install the package. After installation, a subdirectory will appearnode_modules
where the installed module will be located.npm uninstall package
- package removal.
nvm
nvm is the Node.js version manager.
To change the version of Node.js, run the command:
nvm use xx
Instead xx
specify the required version of Node.js, which was previously selected in site settings current hosting account.
package.json
package.json
— configuration file and npm package management.
Options:
name
(mandatory) — the name of the application in small Latin letters without spaces, symbols can be used to separate words-
and_
.description
— application description.dependencies
— section with information about all packages used by the application.
Team npm init
- creates a file from scratch. All necessary data is requested before creation. At the end, you need to familiarize yourself with the contents of the future file and if everything is in order, enter yes
. Note You can just click on all the questions Enter, then a file with standard content will be created.
Node.js settings
Application status
Block «Application status»:
- «Application status» - the current status of the Node.js service:
- «Launched» - the service is up and running.
- «Stopped» — not started (for example, due to the lack of
package.json
) or stopped due to an error.
- «Command to run» — the presence of a script with a command
start
in filepackage.json
:- «Present» - found.
- «Missing» — not found.
- «Force restart» - service restart. Attention! You need to click after any file changes.
- «Forced stop» - stopping the service. The button is displayed only when the application is running.
Main settings
Block «Main settings»:
- Socket path or IP address and port (depending on type of proxy).
- The name of an environment variable containing the path to the socket, or variables containing the port and IP address (depending on type of proxy). Note Pushed to every application when it starts.
- Sample code that should be in an application to listen for site traffic.
Application launch
Block «Application launch» - contains the command to be added to package.json
to run the application (./index.js
in the command - the path to the main application file, the file name can be anything):
Your package.json
Block «Your package.json» - shows the current content of the file package.json
of root directory site (updated when opening or reloading the settings page):
If syntax errors are found in the file in the block, a notification will be displayed «The file contains errors»... The presence of an error notification while the application is running means that the application was launched before the error appeared in the file.
Application logs
Block «Application logs» - displaying application logs in real time. Includes information that the application writes to the console while it is running.
The full log is in the view file ~/.system/nodejs/logs/www.example.com.log
where instead of www.example.com
will be the name of your site.
Application examples
Hello, World!
- Create in root directory site two files:
- Configuration file
package.json
:{ "name": "node", "version": "1.0.0", "scripts": { "start": "node ./index.js" } }
- Application file
index.js
:const HTTP = require('http'); const WebServer = HTTP.createServer((req, res) => { res.statusCode = 200; res.setHeader('Content-Type', 'text/plain'); res.end('Hello, World!'); }); WebServer.listen(process.env.PORT, process.env.HOSTNAME, () => { console.log(`Server is running.`); });
- Click «Restart» in Node.js settings.
- Open the site in a browser. If everything is ok, you will see the text «Hello, World!».
Using npm and express framework
- Connect to hosting via SSH.
- Go to root directory site with a command of the form:
cd example.com/www
Where
example.com
- Name of the site,www
- its subdomain. - Initialize npm and install the express framework with the command:
npm init && npm i express
- Create in root directory site application file
index.js
:const express = require("express"); const app = express(); app.get("/", function(request, response){ response.end("Hello from Express!"); }); app.listen(process.env.PORT);
- Edit the file
package.json
and add a launch command of the form to it:"scripts": { "start": "node ./index.js" }
Observe JSON syntax when adding a command!
The command needs to be added not just to the end of the file, and following the standard JSON format syntax:- Section
scripts
should not be duplicated. If it is already in the file, add the commandstart
there. - There must be a comma at the end of the previous line at the same nesting level.
- The nesting order of the parentheses must be correct.
- Click «Restart» in Node.js settings.
- Open the site in a browser. If everything is ok, you will see the text «Hello from Express!».