Skip to main content

How to use Node.JS

Node.JS based programs can be run in our hosting packages. To run the programs, the following settings must be made on the storage.

USE IN DEFAULT CGI MODE

It is possible to run NodeJS scripts via the #!/bin/env node shebang line and the appropriate Apache handler in cgi mode. If the Node.JS program you want to run is jscrpt, then you need to add the appropriate Apache administrator. Use cPaneles File Manager or an FTP client to connect to the storage space and then log in to the library where you uploaded the files and folders of the program.

Create a .htaccess file and upload it with the following content:

AddHandler cgi-script     .jscript

The setting can be set globally, in this case the entire storage will be valid.

In this case, log on to the cPanel interface and then find the Apache Keypad in the Advanced Settings box.

Advanced / Apache Handlers

Advanced / Apache Handlers

On the page that appears, add the following extension in the Create an Apache Handler section:

Create an Apache Handler

Create an Apache Handler

Handler: cgi-script

Extension(s): .jscript

Click the Add button.

You can test your storage settings by creating the following popup file. Make a file with the text editor (for example, Notepad ++) with the following content:

#!/bin/env node
console.log("Content-Type: text/html;charset=UTF-8\n");
console.log("Hello World!");

Save the file as a test.jscript file under UNIX standard lineup (LF), then upload it to your storage. After uploading, the file must be set to 755 or rwxr-xr-x. You can modify the privilege using the FTP client program. Then, in the browser, call the prepared page (http://yourdomainname.tld/test.jscript) here in Hello World! must be inscribed.

SET UP ALTERNATIVE CGI MODE

After making the settings in this description, the .js files from the browser will be run by the cgi-bin/cgi-node program. To use CGI-Node.js, download the latest release from http://www.cgi-node.org/downloads.

They suggest that you download the cgi-node.min.js file, as this web site provides faster service. Rename the downloaded file to cgi-node, open with a text editor and replace the first line with:

#!/bin/env node

Next, find the SessionPath variable in the file. Set a variable as a directory that can not be accessed through a browser. For example: /home/YOUR_CPANEL_USER_NAME/tmp/cgi-node-session. Then save the file and then upload it to your cgi-bin folder. Also set a 755 privilege for the file.

If you specify a non-existent directory in the file, you will create it on your storage after uploading it.

Next, create a .htaccess file in the root directory of the web site with the following content.

Action     cgi-node     /cgi-bin/cgi-node
 AddHandler cgi-node     .js

To test your settings, you need to create a file with a text editor in your site’s root directory. The file contains the following content:

CgiNodeInfo();

For example, save the created file, for example, as body.jscript, then call the page (http://yourdomainanme.tld/test.jscript). Your browser will display a spreadsheet that will show the variables in the run environment.

Node.JS test page

Node.JS test page