Thursday, September 29, 2016

How to push node.js app to heroku


1) Download and install Heroku CLI. (For Mac : https://devcenter.heroku.com/toolbelt-downloads/osx)

2) Verify your installation
- heroku --version



3) Create free heroku account by visiting https://signup.heroku.com/signup/dc

4) Login to heroku from command line using the account that you have created above.
- heroku login (Enter registered email id and password)


5) Node, NPM and Git should be installed.


6) Create a sample Node.js app

i) Initialize (It will create package.json file)

- mkdir sample-node-project
- npm init (Follow the steps and enter few details )


ii) Install express.js

- npm install --save express

Check the package.json file, express.js should be added as dependancy. 

sample-node-project raja$ cat package.json 
{
  "name": "sample-node-project",
  "version": "1.0.0",
  "description": "test-example",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [
    "examples"
  ],
  "author": "praja",
  "license": "ISC",
  "dependencies": {
    "express": "^4.14.0"
  }
}

iii) Create Server

- sample-node-project raja$ vi index.js
- Add the following line into index.js

var express    =    require('express');
var app        =    express();
var server     =    app.listen(3000,function(){
    console.log("We have started our server on port 3000");
});

* Update values for "scripts" key as following in package.json

"scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "start": "node index.js"
  }

iv) Start the server

- npm start


v) Open the browser and type http://localhost:3000
- A blank page ( Cannot GET / ) will be displayed as we haven't set any route yet.

vi) Set the Router 

Add the following line before server start.

app.get('/',function(req,res){
   res.send('Hello world');
});















vii) Stop and Start the server again.
viii) Go to http://localhost:3000
You should see "Hello World". As the router is set, so server is responding with "Hello world" response. 


7) Push the existing node.js project to GIT.


i) git init
ii) git status


iii) create .gitignore file add the following lines.
# Node build artifacts
node_modules
npm-debug.log

iv) git status
v) git add .
vi) git commit -m "First Commit - Node Sample"


vii) Create a new repository in https://github.com. Check the repository URL. (For ex: https://github.com/sampleuser/FST-Node-Sample.git)

viii) git remote add origin "remote repository url"

ix) git push origin master




Final Changes before pushing node.js project to Heroku

8) Add node.js engine details in package.json so that both local and heroku environment will be on sync.

- Modify package.json, add node.js engine details (you can get the version by typing "node -v" in terminal)

"engines": {
    "node": "6.3.0"
}


9) Specify a start script for heroku. 

Heroku first looks for a Procfile. If no Procfile exists for the app, it will attempt to start a default web process via the start script in package.json.

Create a file name called "Procfile". 
Add the below content.

web: node index.js 


10) We can not hardcode the server listening port (local env : 3000) for heroku. So modify the app listen code in index.js with the following.

var server     =    app.listen(process.env.PORT || 3000,function(){
    console.log("We have started our server on port 3000");
});


11) Build the project and run it locally and load the browser with http://localhost:3000

App should run properly. "Hello World" should be seen.

12) All the latest changes should be pushed to git before the project goes to heroku. 

** Push all modified code changes to git.


Final Code Structure :






Create App in Heroku

13) Login to heroku with your free account that you have created in the first place.

https://id.heroku.com/login



14) Create a new app from heroku dashboard by clicking [New -> Create new app]




15) Give your application name and click "Create"





16) Check the GIT Url of the application from the settings tab.


 



17) Go to the terminal of your nodejs project. Execute the following command.

 $ git remote add fstheroku https://git.heroku.com/fast-sample-nodejs-app.git



18) Now push the code to heroku

$ git push fstheroku master































19) Open the deployed url in the browser. In my case it is https://fast-sample-nodejs-app.herokuapp.com/



Thursday, September 22, 2016

How to connect to AWS from Mac using SSH

Follow the steps to connect to AWS from Mac

- Find the full path of the .PEM file which has the private key (You must have got it while creating the the aws instance)
- Open the Mac Terminal and execute the following command.

ssh -i /home/user1/.ssh/aws.pem root@aws_instance_ip

Note:
If you have already used a .PPK file in your windows system then follow the steps below to generate the .PEM file.

- Check if puttygen is installed

$ puttygen --version

puttygen: Release 0.67

- If puttygen is not installed then install putty using brew.
puttygen will come with putty.

  * Install Ruby in your Mac box.
  * Run the following command :

    /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

  <More info on brew from http://brew.sh/ site>

  Once brew is installed, install putty

  $ brew install putty

- Check if puttygen is installed

$ puttygen --version

puttygen: Release 0.67

- Output the data from .PPK file to .PEM file using puttygen

$ puttygen aws.ppk -0 private-openssh -o aws.pem

Once you generated the .PEM file connect to aws instance using ssh.
ssh -i /home/user1/.ssh/aws.pem root@aws_instance_ip