hosting node js app on godaddy shared code example
Example: how to host node js application on godaddy
Yes this is possible. Somehow I have never seen anyone actually answer this question correctly. This works with the most basic shared hosting plans. I have successfully been able to set it up a couple different ways. I think the second is probably what you want :
1. cgi-node http://www.cgi-node.org/home
Basically this replaces PHP on the lamp stack. You can run javascript through node like you would run PHP. This has all the same functionality of node js but is only really geared towards template rendering.
<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>
OR
2. Standalone Server (this works with NameCheap hosting and GoDaddy shared hosting)
In your shared hosting account you will need SSH in order to do this. So you may need to upgrade or request SSH access from their customer support. Download the latest NodeJS https://nodejs.org/en/download/. The shared hosting is probably in linux 64 bit. You can check this on linux or unix by running :
uname -a
Download the Linux binaries and put the bin/node (and the bin/npm file if you want to use npm on the server) file from the download in /home/username/bin/ (create the bin folder if it doesn't exist) on the server. Put permissions 755 on the node binary. So you should have a new file here :
/home/username/bin/node
Open up the .htaccess file in /home/username/public_html and add the following lines :
RewriteEngine on
RewriteRule (.*) http://localhost:3000/$1 [P,L]
Create a file in /home/username/public_html and just call it app.js. Add the following lines in that file :
const http = require('http');
const hostname = '127.0.0.1';
const port = 3000;
const server = http.createServer((req, res) => {
res.statusCode = 200;
res.setHeader('Content-Type', 'text/plain');
res.end('NodeJS server running on Shared Hosting\n');
});
server.listen(port, hostname, () => {
console.log(`Server running at http://${hostname}:${port}/`);
});
SSH into the server run these commands :
cd /home/username/public_html
which node # this should return ~/bin/node
node app.js & # This will create a background process with the server running
If you can get this set up right this will save you a ton of money in the long run as opposed to using something like AWS or Heroku etc.