Replace PHP with JavaScript using Node.js!!!

What is CGI-Node?

CGI-Node provides the ability to run JavaScript on any web server just like PHP as a CGI using Node.js. 

Essentially allowing developers to use a single programming language, JavaScript, for both client and server. You can even run the same code (libraries) on the client or server, reducing development time and code base.

CGI-Node run on shared hosting sites running Apache. It can run along side PHP.

Can I use it with my existing hosting service?

CGI-Node was designed specifically for this purpose. It works on any web hosting service that allows custom CGI scripts. In most cases if your web hosting company uses cPanel then CGI-Node will work.

See this tutorial to get CGI-Node working on GoDaddy.

Is it easy to setup?

If you can follow simple instruction then YES. See our GoDaddy tutorial here
But, if you're a pro then here is the simplified version:
  • Download the appropriate Node.js binary from here and copy it to your bin folder on your site.
  • Download cgi-node.js from here and copy it to your cgi-bin folder on your site.
  • Update the first line within cgi-node.js to point to the location of the node executable you uploaded earlier.
  • Ensure you have .htaccess file that contains the following information:
Action cgi-node /cgi-bin/cgi-node.js 
AddHandler cgi-node .jss

That's it!

Can I access the HTTP Request, forms, post data, etc?

YES, CGI-Node pre-processes all the request headers, query strings, POST data and provides a clean and simple API to access every thing. See the API here.

Like phpInfo(), cgiNodeInfo() will display all the server variables, request headers, query string, POST forms and POST files. 


Here is a quick look some of CGI-Node API:

// Writes the given string back to the browser.
write(chunk)

// Includes other .js or .jss files within the current file.
include(path)

// Provides full access to the URL. This object is the Node.js url object.
request.url

// This object contains parsed query string variables. {name: value}
request.query

// This object contains the HTTP request header.
request.header

// This object provides access to the parsed HTTP body (POST)
request.post.form
request.post.files

// This object contains all the response headers.
// You can add, read, or remove headers before sending them to the client.
response.headers

// This method sends the current headers to the client.
response.sendHeaders()

Can I use PHP style coding with HTML?

If you have used PHP before then CGI-Node works exactly the same way. Any requested file from your web-site with .jss extension will be executed on the server using Node.js. CGI-Node uses the same PHP tags to embed JavaScript between your HTML code.

NOTE: Unlike PHP, if you include a .js file then you don't need to wrap the code within <?  ?> tags. This will allow you to use the same files on the client/browser without getting syntax errors. This is especially helpful if you have core library or utility methods that can be shared between the server and client.

Here is an example:

<html>
    <body>
<?
    var helloWorld = 'Hello World!'; 
    write(helloWorld + '<br/>'); 
?>
        <?= helloWorld ?>
    <br/>
    <b>I can count to 10: </b>

<?
    for (var index= 0; index <= 10; index++) write(index + ' ');  ?>
    <br/>
<b>Or even this: </b>
<?  
    for (var index= 0; index <= 10; index++) { ?>

        <?= index ?> 
<? } ?>

    </body>
</html>

Results:

Hello World!
Hello World! 
I can count to 10: 0 1 2 3 4 5 6 7 8 9 10 
Or even this: 0 1 2 3 4 5 6 7 8 9 10

Is there any session management?

Yes, CGI-Node automatically creates a new session for new users and handles saving and loading session.

Simply store all your session data in session.data and it will persist after every request. See documentation for full details here.

Do I have access to the NPM libraries?

Absolutely, this works the same way as running Node.js locally. You can even use the require() method the same way. For more information about NPM go here: https://www.npmjs.org/

Can I debug Node.js code running on the server?

YES!!! this feature is planned and will be release in subsequent versions.

Can I access MySQL from CGI-Node?

Yes, simply install the MySQL module from NPM.
If you're not sure how, I'l be writing a tutorial on this soon.

Is there anything I can't do with CGI-Node?

YES, cgi-node can not cook you breakfast. YET!