Stop and Remove all docker containers and images

Published at July 11th, 2016 in Cloud Computing

Some important commands for Docker containers and images. It’s useful for delete all docker containers and images altogether at once.


# Stop all docker containers
docker stop $(docker ps -a -q)

#Remove all docker containers
docker rm $(docker ps -a -q)

# Delete all images
docker rmi $(docker images -q)

Create NodeJS – ExpressJS static file server in a minute

Published at July 4th, 2016 in AngularJS, ExpressJS, NodeJS

It’s pretty straight-forward tutorial for anybody to understand. Suppose, you want to run your AngularJS static application in your local PC with pretty URLs and other features. You may need to create a stand-alone static file server.

So here you can do that with NodeJS and Express.

– Just create a package.json file in your application root. and write the following json configuration there.


{
  "scripts": {
    "server": "node static-server.js"
  },
  "dependencies": {
    "express": "^4.14.0",
    "node-static": "^0.7.7",
    "path": "^0.12.7"
  }
}

And after that create a JS file name ‘static-server.js’ with the following code where ExpressJS will serve you static files.


var express = require('express');
var app = express();

app.use(express.static(__dirname + '/public'));

app.get('*', function(request, response, next) {
    //__dirname + '/public' is your main file location of a
    response.sendfile(__dirname + '/public');

    //if you want to run AngularJS app with html5Mode(true) 
    //then you have to run server via index.html
    //response.sendfile(__dirname + '/public' + '/index.html');  
    //Write your own angularJS index file location
});

app.listen(8081, function(){
    console.log('Starting server at http://localhost:8081')
});

Now you are all set. Now install all nodejs dependency libraries by npm install command and then run ‘npm run server’ command to start your static server. After successfully started (can see in console) you can visit your static web server from http://localhost:8081 (you can change port as your wish). I think this will help you and your team.

AngularJS and API Backend via nginx proxy – Sample nginx virtual host

Published at July 4th, 2016 in AngularJS

If you want to run your AngularJS front-end web application and run nginx as your API proxy server, then the following nginx virtual host configuration can help you.


#AngularJS frontend with proxied API backend
server {
        listen 80;
        server_name your-angularjs-public.url;

        root your/angularjs/app/root-directory;
        index index.html;

        location / {
                # If you want to enable html5Mode(true) in your angularjs app for pretty URL
                # then all request for your angularJS app will be through index.html
                try_files $uri /index.html;
        }

        # /api will server your proxied API that is running on same machine different port
        # or another machine. So you can protect your API endpoint not get hit by public directly
        location /api {
                proxy_pass http://api-server-url;
                proxy_http_version 1.1;
                proxy_set_header Upgrade $http_upgrade;
                proxy_set_header Connection 'upgrade';
                proxy_set_header Host $host;
                proxy_cache_bypass $http_upgrade;
                proxy_set_header X-Real-IP $remote_addr;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        }

        #Static File Caching. All static files with the following extension will be cached for 1 day
        location ~* .(jpg|jpeg|png|gif|ico|css|js)$ {
                expires 1d;
        }
}

This will help you lot to configure your Nginx proxy for your API backend and run your API with AngularJS based web application interface.

Start forever node command after reboot

Published at July 3rd, 2016 in NodeJS

Unfortunately by default forever will be terminated in every reboot of your system. So if you want to restart your forever script after every reboot.

Then you can simply handle that with crontab @reboot attribute. Here is the crontab command you need simply.


@reboot /usr/local/bin/forever start yourNodeJSAppStartFile.js

mongodump usage for MongoDB backup and restore

Published at July 3rd, 2016 in Database

According to MongoDB Backup manual, mongodump is a very handy tools for MongoDB administrator to backup and restore full mongodb cluster or single database or single collection.

mongodump usage

Here is a very simple command to take backup or restore mongodb system.

Backup

 
sudo mongodump --host hostName -d databaseName --port portNumber --username userName --password passWord --out directoryLocation

with the above command you can take backup of your database. I don’t need to explain all parameters as I wrote variable name very clearly.

Restore

 sudo mongorestore --host hostName -d databaseName --port portNumber --username userName --password passWord backupSourceLocation 

I guess it will help you quickly.

Pages »