Introduction
Node.js is essentially code executed on our servers to host, create and control web applications using this type of language.
In the following, we will focus on information for beginners. We will start by explaining how to access Node.js in N0C and we will provide a detailed example of launching and modifying an application using this interface.
Then, for more advanced users, we will explain how to do these same operations in command line.
Documentation
Documentation on Node.js can be found on the Web, in particular under: https://nodejs.org/en/docs/guides/.
You can also refer to this site for more information: https://docs.cloudlinux.com/command-line_tools/#node-js-selector.
Debugging Applications
When an error occurs, a page like the one below is displayed:
This page is not very explanatory. Activating the development mode allows you to investigate (how).
Prerequisite
Enter the following address in your web browser: https://mg.n0c.com/en/.
Access to Node.js in N0C
- In the Sidebar menu, select Languages > Node.js.
- Applications already set up are displayed in the interface:
Implementation of a New Node.js Application with N0C
To explain the approach more easily, we will proceed with an example.
Let’s say you want to create the app2.js application that displays the words “Hello World! NodeJS”. Three steps are necessary:
- First of all, you have to create an application in the directory intended to contain it (we will call this directory “app2” as an example, but the user can give it the name they desire). When it is executed, the application displays the words “It works!”.
- Then you have to program it appropriately (in our example, so that it displays “Hello World! NodeJS”) and create a “package.json” file.
- Finally, you have to invoke the application environment.
Step #1 – Creation of an Application with Nodejs
Make sure the app2 directory is created (how).
- Click on the Create button:
- Choose the desired Node.js VERSION :
- Indicate in which APP DIRECTORY you want the application to be created (in our example we would enter app2).
- Indicate under APPLICATION DOMAIN / URL from which URL the application should be accessible (in our example, we would write app2).
- Indicate the BOOT FILE (optionnal).
- Click on the CREATE BUTTON. A message confirming the success of the creation is then displayed.
- Confirm the creation of app2.js by opening the app2 directory in the File Manager (how).
- Test Node.js application by typing in your browser https://${yourdomain}/app2, ${yourdomain} having to be replaced by your real domain name.
- The result is (as expected): « It works ! ».
Step #2 – Program the Application
Now that the Node.js application is installed in the directory to be used and functional, you can program it as you wish and improve the environment with the setting files package.json and the npm package manager.
A “package.json” is a JSON file that exists at the base of a Node.js project. It contains metadata related to the project and is used to manage dependencies, scripts, versions and various other aspects of the project.
Here are the steps to program:
- In the File Manager, open the directory (in our example, app2) and edit the code app2.js (how).
- Add the application configuration to the app2.js file. In our example, copy the following code in the programming window of the File Manager (how), so that the words “Hello World! NodeJS” will be displayed when the user runs app2 on your domain:
const http = require('http') const hostname = '127.0.0.1'; const port = 'passenger'; const yourdomain = 'yourdomain.com' const server = http.createServer((req, res) => { res.statusCode = 200; res.setHeader('Content-Type', 'text/plain'); res.end('Hello World! NodeJS'); }); server.listen(port, hostname, () => { console.log(`Server running at http://${yourdomain}/`); });
- In the code, replace ${yourdomain} by the name of your domain name.
- Save the code.
- At this point, restart the application, as explained in the paragraphRestart a Node.js Application.
- Test the application by typing in your browser via https://${yourdomain}/app2. The result shall now be “Hello World! NodeJS”.
Install Dependencies in the « package.json » File
In order to install the dependencies of your project, make sure that your package.json is at the root of the project. Then :
- Identify the application (app2 in our example) and click on the Edit icon:
- Install the NPM module by clicking on the Install button:
Step #3 – Invoke the Application Environment
Before performing any other command (for example,npm
or node
), you must call the application environment by running the following command:
The command must be copied into the shell of the account in question. It is possible to connect via SSH.
To see the command, you must edit the application (please refer to the paragraph NodeJS Application Settings Modification).
So, in our example, the command copied is:
source /home/fbkfdayw/nodevenv/app2/11/bin/activate
Where :
- “fbkfdayw” shall be replaced by your Username;
- “app2” shall be replaced by the name of your application; and
- “11” shall be replaced by the Node.js version of your application.
You can consult the documentation available at https://nodejs.org/en/docs/ to help you debug your code.
Actions on application with N0C
Restart a Node.js Application
Sometimes it is necessary to restart an application as, for example, when you modify the code.
- Click on the Restart icon besides the application to activate:
- A message shall be displayed to confirm the successful execution of the command.
Node.js Application Version Modification
- Click on the Change version icon next to the application to modify:
- Select the desired VERSION NODEJS:
- Click on the EDIT button.
Node.js Application Settings Modification
- Identify the application you want to modify and click on the Edit icon:
- Change the desired settings:
- Install the npm module by clicking on the Install button.
- Click the EDIT button to save your changes.
Stopping and Starting an Application
It is possible to stop an application by clicking on the Stop icon:
It is possible to start an application by clicking on the Start icon:
Delete a Node.js Application
- Identify the application and click on the Delete icon:
- At the command prompt, confirm the deletion.
Implementation of a New Node.js Application in Command Line
For more advanced users, the command line approach can be used.
The following steps have to be followed in order:
- Connect in SSH.
- Find the availble Node.js versions.
- Create the functional base application at the CloudLinux level.
- Set up the application.
Note
The json format is supported only as a command return. So, it is possible to format the return by redirecting the stdout to python -m json.tool. Example:
/usr/sbin/cloudlinux-selector get --json --interpreter=nodejs | python -m json.tool
Step #1 – Connect in SSH
Connect in SSH (how).
For example, if you are using the Apple MacOSX environment, you can enter:
ssh < USER >@ < DOMAIN > -p PORT
Where :
- USER : Your username.
- DOMAIN : The name of the domain (make sure it points to the hosting).
- PORT : Port to use for hosting (unless otherwise specified, use 5022).
At the command prompt, provide your password.
Step #2 – Find the Available Node.js Versions
This command will return the versions of Node.js available on the server:
$~ /usr/sbin/cloudlinux-selector get --json --interpreter=nodejs
Step #3 – Create the Basic Functional Node.js Application at CloudLinux Level
The following command creates a functional database:
$~ /usr/sbin/cloudlinux-selector create --interpreter=nodejs --json --app-root=<APP_ROOT> --app-uri=<APP_URI> --app-mode=production --version=<VERSION> --domain=<DOMAIN> <STARTUP_FILE>
Where:
- APP_ROOT: The directory where you want to install your application (keep this variable for future commands). Example: /home/user/applicationnode1.
- APP_URI: The path to your application after your domain. Example: /applicationnode1 would become following url: domain.com/applicationode1.
- VERSION: An available version (as per the return command previously seen). Exemple: 9.11.
- DOMAIN: The domain where you want to run your application. Example: domain.com.
- STARTUP_FILE: The name of the file to enter into your application: app.js by default. Example: index.js.
The expected return is:
{"result": "success", "timestamp": 1596731243.1282492}
You now have a functional Node.js application if « status=Ok » is displayed.
Otherwise, you should see the error message and correct the order accordingly.
Step #4 – Setup the application
Upload your application to the directory where you installed your application (<APP_ROOT>) including your package.json.
Once your files are in place, you can install the NodeJS packages using the following command:
$~ /usr/sbin/cloudlinux-selector install-modules --json --interpreter=nodejs --app-root=<APP_ROOT>
Where the variable APP_ROOT is the directory where you want to install your application. Example : /home/user/applicationnode1.
Actions on Application in Command Line
It is possible to start, stop or restart an application, and much more. You just have to enter the appropriate command:
– To start :
$~ /usr/sbin/cloudlinux-selector (start) --json --interpreter=nodejs --app-root=<APP_ROOT>
– To stop :
$~ /usr/sbin/cloudlinux-selector (stop | destroy) --json --interpreter=nodejs --app-root=<APP_ROOT>
– To restart :
$~ /usr/sbin/cloudlinux-selector (restart) --json --interpreter=nodejs --app-root=<APP_ROOT>
– To destroy :
$~ /usr/sbin/cloudlinux-selector (destroy) --json --interpreter=nodejs --app-root=<APP_ROOT>
It is also possible to execute scripts that are recorded at the package.json level:
$~ /usr/sbin/cloudlinux-selector run-script --json --interpreter=nodejs --app-root=<APP_ROOT> --script name=<SCRIPT_NAME> -- --<OPTION_1> <ARG1> <ARG2>
Where the following variables are use:
SCRIPT_NAME : the name of the script in your package.json.
OPTION_1, ARG1 et ARG2 : script arguments.
Command Line Logout
To logout, enter the following command:
exit
How to Transfer Logs to N0C Storage
If you wish to transfer logs from a NodeJS application to N0C Storage, please refer to this JavaScript script: https://github.com/PlanetHoster/transfer-logs-to-n0c-storage-demo.
By transferring logs from your server to N0C Storage, you save space and avoid cluttering up your SSD, while keeping your logs accessible.
This script can be used as a starting point for more complex scripts, or integrated directly into an application. In the example used in the script, a cron job is set up to run the script twice a day.