Knowledgebase

How to Create a Node.JS Application (Legacy Method)

This article advises you on how to create a node.js application using a cron jobs and php. This is no longer necessary you can do it from our own interface and there exists a guide at https://support.hawkhost.com/index.php?/Knowledgebase/Article/View/197/0/how-to-create-a-nodejs-application . However for users using this method it remains accessible for their reference.

At Hawk Host it is possible to create persistent Node.JS applications utilizing several of our built in features.  We will walk you through how to create a node.js hello world but the same can be applied to applications such as Ghost

1) You'll need to create a nodejs folder within your accounts home directory which can be completed by FTP/SFTP or throuhg the cPanel file manager.  After this is created you will create an app.js file inside your nodejs folder with the following contents:

var http = require('http');

http.createServer(function (request, response) {
    response.writeHead(200, {'Content-Type': 'text/plain'});
    response.end('Hello World\n');
}).listen('12345');

console.log('Server started');


Replace 12345 with a randomly generated 5 digit number and record this as it'll be necessary later on.

2)  You now need to create a PHP file which will be used to manage your application.  Create a cron.php file with the following contents:

<?php

$host = '127.0.0.1';
$port = '12345';
$checkconn = @fsockopen($host, $port, $errno, $errstr, 5);
if(empty($checkconn)){
     exec('/usr/bin/node /home/'.get_current_user().'/nodejs/app.js > /dev/null 2>&1 &');
}


Change the 12345 to contain the port number you randomly generated previously.

3)  You will now need to login to cPanel and open the cron jobs option



You will need to create a cron job with the following settings:




The cronjob command is

/usr/local/bin/php /home/username/nodejs/cron.php > /dev/null 2>&1

Replace username with your accounts username.

4) The final step is to make it possible for the web server to connect to your Node.JS application.  Navigate to where you wish for your application to load from which in our case will be the public_html folder.  Create a .htaccess file with the following contents:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ http://127.0.0.1:12345/$1 [P,L]


Replace 12345 with your random port you generated and upon visiting the location your Node.JS application should now load.
  • 0 Users Found This Useful
Was this answer helpful?

Related Articles

Available Node.js Versions

We recommended all Node.js applications are created using our Setup Node.js Application...

Available Python Versions

We recommended all Python applications are created using our Setup Python Application interface....

Available Ruby Versions

We recommended all Ruby applications are created using our Setup Ruby Application interface. If...

Default permissions on folders and PHP files.

We currently run Litespeed Web Server which runs PHP as the user so files and folders should...

Do you support Ruby on Rails?

Yes we support Ruby On Rails and you can install and utilize many different versions of Ruby and...