Just created your node application and dont know what to do with that further ? Well your application would be of no use to others if you are not gonna host it online somewhere. But hosting a node js application is kinda different compared to hosting your PHP application. You just dont have any file manager that would let you upload your node js and you could not run it. It doesn’t work that way. So how do you do it ? We would be dealing the exact same thing in this post on how to host your Node JS application online.
We would be using IBM Bluemix to host your node js application. IBM Bluemix is a cloud hosting platform that lets you to host your applications online. It accepts loads of different languages including even PHP. You could upload your PHP application too. But still, this is in beta as of now and it is free unless it is beta. Maybe they would have a free plan to go with, including loads of limitations. So now lets see on how you would host your Node application online using IBM Bluemix.
Hosting your First Node JS Application :
1. Head over to this link
and join the free beta. Sign up for it and move on to your dashboard.
2. The next thing that you would need to do is to download and install the cloud foundry tool that is a command line tool for IBM Bluemix. You could get that from this GitHub link
. Scroll down to the downloads section.
For Windows users just copy the downloaded file which would be name cf to your system32 folder so that the command could be accessed via the command prompt.
For Ubuntu users I hope that tool is available via the Ubuntu Software Centre.
3. Now that you have downloaded and installed the Cloud Foundry tool open up your Terminal and type in the following command so that the api gets set.
cf api https://api.mybluemix.net/
I hope that it would already be set for Windows users but still there is no harm in running the above code.4. Make a copy of your Node application folder and open it up. You need to create a file named manifest.yml
in your root (make note of the extension). And open it up in your text editor and type in the following in it.
– name: NAME OF YOUR APPLICATION HERE
host: YOUR PREFERRED HOST
command: COMMAND YOU NEED TO RUN TO START THE APPLICATION
Make sure that you replace the things written in caps. Name of your application MUST match the name of the folder in which your Node application resides.YOUR PREFERRED HOST – This is where your Node application would reside online. If you enter in xproweb
then your application would be hosted at xproweb.mybluemix.net
given the fact that it is not taken by some other user. If it had been taken you will get an error while upload and we will be uploading the file now.For example my manifest.yml
file for the chat application which I created in the last post looked like this :
– name: xproweb
command: node server.js
5. Alright now that you have created your manifest file, open up your terminal and change directory to your Node application root folder. And type in the following command :
It would now prompt you for an Email ID and a password. Enter in the email and password that you used to sign up at Bluemix.
6. Now you need to make a small change to your code. Add the following snippet before everything in your server.js file. This is a must for every application.
var port = (process.env.VCAP_APP_PORT || 8192);
var host = (process.env.VCAP_APP_HOST || ‘localhost’);
7. Before you upload your application you MUST delete your node_modules folder. Cloud Foundry will look into your package.json file and it would install the dependencies by itself.
8. And its time to upload your application to Bluemix. Type in the following command in your terminal and hit enter to start upload.
cf push app_name
Replace app_name with the name of your application as specified in your manifest.yml file. So now your application would be uploaded successfully. Just point your browser to the host provided in your manifest.yml file and you would see your application there !
There is also an alternative to Bluemix called as Heroku and you could check about it here.
That is it ! You now have a successful Node application running online ! Happy Node-ing