Sunday, July 14, 2019

JSON





  • JSON: JavaScript Object Notation.
  • JSON is a syntax for storing and exchanging data.
  • JSON is text, written with JavaScript object notationJSON


The JSON object, available in all modern browsers, has two very useful methods to deal with JSON-formatted content: parse and stringify. JSON.parse() takes a JSON string and transforms it into a JavaScript object. JSON.stringify() takes a JavaScript object and transforms it into a JSON string.


  1. When exchanging data between a browser and a server, the data can only be text.
  2. And we can convert any JavaScript object into JSON, and send JSON to the server.
  3. We can also convert any JSON received from the server into JavaScript objects.
  4. This way we can work with the data as JavaScript objects, with no complicated parsing and translations.

convert to JSON :-

const myObj = {
 name: 'Skip',
 age: 2,
 favoriteFood: 'Steak'
};
const myObjStr = JSON.stringify(myObj);
console.log(myObjStr);
// "{"name":"Skip","age":2,"favoriteFood":"Steak"}"
console.log(JSON.parse(myObjStr));
// Object {name:"Skip",age:2,favoriteFood:"Steak"}


Sending Data:-
If you have data stored in a JavaScript object, you can convert the object into JSON, and send it to a server:

var myObj = {name: "John", age: 31, city: "New York"};
var myJSON = JSON.stringify(myObj);
window.location = "demo_json.php?x=" + myJSON;

 Recieving Data:-

If you receive data in JSON format, you can convert it into a JavaScript object:

var myJSON = '{"name":"John", "age":31, "city":"New York"}';
var myObj = JSON.parse(myJSON);
document.getElementById("demo").innerHTML = myObj.name;
 Storing Data:-

When storing data, the data has to be a certain format, and regardless of where you choose to store it, the text is always one of the legal formats.
JSON makes it possible to store JavaScript objects as text.
// Storing data:
myObj = {name: "John", age: 31, city: "New York"};
myJSON = JSON.stringify(myObj);
localStorage.setItem("testJSON", myJSON);

// Retrieving data:
text = localStorage.getItem("testJSON");
obj = JSON.parse(text);
document.getElementById("demo").innerHTML = obj.name;

Why use JSON?

Since the JSON format is text only, it can easily be sent to and from a server, and used as a data format by any programming language.

JavaScript has a built in function to convert a string, written in JSON format, into native JavaScript objects:

JSON.parse()

So, if you receive data from a server, in JSON format, you can use it like any other JavaScript object.


What is JSON?

JSON stands for JavaScript Object Notation
JSON is a lightweight data-interchange format
JSON is "self-describing" and easy to understand
JSON is language independent

JSON Values

In JSON, values must be one of the following data types:


  • a string
  • a number
  • an object (JSON object)
  • an array
  • a boolean
  • null


In JavaScript, values can be all of the above, plus any other valid JavaScript expression, including:

a function
a date
undefined

In JSON, string values must be

In jSON:- { "name":"John" }
In js  :- { name:'John' }


JSON Uses JavaScript Syntax

Because JSON syntax is derived from JavaScript object notation, very little extra software is needed to work with JSON within JavaScript.

With JavaScript you can create an object and assign data to it, like this:

var person = { name: "John", age: 31, city: "New York" };

The same way JavaScript objects can be used as JSON, JavaScript arrays can also be used as JSON.

JSON.Parse() 
A common use of JSON is to exchange data to/from a web server.
When receiving data from a web server, the data is always a string.
Parse the data with JSON.parse(), and the data becomes a JavaScript object.



JSON From the Server
You can request JSON from the server by using an AJAX request

As long as the response from the server is written in JSON format, you can parse the string into a JavaScript object.

EX:
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
  if (this.readyState == 4 && this.status == 200) {
    var myObj = JSON.parse(this.responseText);
    document.getElementById("demo").innerHTML = myObj.name;
  }
};
xmlhttp.open("GET""json_demo.txt"true);
xmlhttp.send();



Parsing Dates

Date objects are not allowed in JSON.

If you need to include a date, write it as a string.

You can convert it back into a date object ter:


Parsing Functions

Functions are not allowed in JSON.

If you need to include a function, write it as a string.

You can convert it back into a function later:

JSON.Stringify()
A common use of JSON is to exchange data to/from a web server.

When sending data to a web server, the data has to be a string.

Convert a JavaScript object into a string with JSON.stringify()

JSON.OBJECT


JSON objects are surrounded by curly braces {}.

JSON objects are written in key/value pairs.

Keys must be strings, and values must be a valid JSON data type (string, number, object, array, boolean or null).

Keys and values are separated by a colon.

Each key/value pair is separated by a comma.


myObj = "name":"John""age":30"car":null };
x = myObj.name;

Arrays as JSON objects


Arrays in JSON are almost the same as arrays in JavaScript.

In JSON, array values must be of type string, number, object, array, boolean or null.

In JavaScript, array values can be all of the above, plus any other valid JavaScript expression, including functions, dates, and undefined.

{
"name":"John",
"age":30,
"cars":[ "Ford""BMW""Fiat" ]
}

JSON PHP

A common use of JSON is to read data from a web server, and display the data in a web page.

This chapter will teach you how to exchange JSON data between the client and a PHP server.

PHP has some built-in functions to handle JSON.

Objects in PHP can be converted into JSON by using the PHP function json_encode():


Saturday, July 6, 2019

JQuery

Image result for Jquery
jQuery is a JavaScript Library.  jQuery greatly simplifies JavaScript programming. 

What is jQuery?
jQuery is a lightweight, "write less, do more", JavaScript library.
The purpose of jQuery is to make it much easier to use JavaScript on your website.
jQuery takes a lot of common tasks that require many lines of JavaScript code to accomplish, and wraps them into methods that you can call with a single line of code.
jQuery also simplifies a lot of the complicated things from JavaScript, like AJAX calls and DOM manipulation

The jQuery library contains the following features:

  • HTML/DOM manipulation
  • CSS manipulation
  • HTML event methods
  • Effects and animations
  • AJAX
  • Utilities

Why jQuery?
There are lots of other JavaScript frameworks out there, but jQuery seems to be the most popular, and also the most extendable.


Many of the biggest companies on the Web use jQuery, such as:

  1. Google
  2. Microsoft
  3. IBM
  4. Netflix

There are several ways to start using jQuery on your web site. You can:

  • Download the jQuery library from jQuery.com
  • Include jQuery from a CDN, like Google

 jQuery from a CDN

<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
</head>


jQuery Syntax

The jQuery syntax is tailor-made for selecting HTML elements and performing some action on the element(s).
Basic syntax is: $(selector).action()

  • A $ sign to define/access jQuery
  • A (selector) to "query (or find)" HTML elements
  • A jQuery action() to be performed on the element(s)Examples:
  1. $(this).hide() - hides the current element.
  2. $("p").hide() - hides all <p> elements.
  3. $(".test").hide() - hides all elements with class="test".
  4. $("#test").hide() - hides the element with id="test"

MongoDB

Image result for mongodb



What is MongoDB-----:-

MongoDB is a document database with the scalability and flexibility that you want with the querying and indexing that you need

⧫    MongoDB stores data in flexible, JSON-like documents, meaning fields can vary from document to document and data structure can be changed over time

⧫ The document model maps to the objects in your application code, making data easy to work with

⧫ You can make developments easily


Why Use MongoDB?

📄  Document Oriented Storage − Data is stored in the form of JSON style documents.

📄  Index on any attribute

📄  Replication and high availability

📄  Auto-sharding

📄  Rich queries

📄  Fast in-place updates

📄  Professional support by MongoDB

Where to Use MongoDB?

  • Big Data
  • Content Management and Delivery
  • Mobile and Social Infrastructure
  • User Data Management
  • Data Hub

In sql data save as table Mongodb saves as collections.










Basic Commands for the MongoDB

Installation Commands (Ubuntu) :

#1
sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 9DA31620334BD75D9DCB49F368818C72E52529D4


#2
echo "deb [ arch=amd64 ] http://repo.mongodb.com/apt/ubuntu bionic/mongodb-enterprise/4.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-enterprise.list

#3
sudo apt-get update

#4
sudo apt-get install -y mongodb-enterprise


How to Start 




#Step 1: Start MongoDB  .
sudo service mongodb start
 #Stop MongoDB
sudo service mongodb stop
#  To Start the Server
sudo mongo
# To Start the Client
sudo mongod



Basic Commands 



1. Finding the current database you’re in 

db 

2. Listing databases

Show dbs



3. Go to a particular database
use Database_name

4. Creating a Database
use Database_name     :- use command used for both action

5. Creating a Collection
db.createCollection("myCollection")


6. Inserting Data
db.myCollection.insert({"name": "john", "age" : 22, "location": "colombo"})
 db.myCollection.insertOne(i      {       "name": "navindu",        "age": 22      })
 db.myCollection.insertMany([      {        "name": "navindu",         "age": 22      },         {        "name": "kavindu",  "age": 20      },    { "name": "john doe",  "age": 25,        "location": "colombo"}     ])
7. Querying Data
db.myCollection.find()
 db.myCollection.find().pretty()

8. Updating documents
db.myCollection.update({age : 20}, {$set: {age: 23}})
db.myCollection.update({name: "navindu"}, {$unset: age});

9. Removing a document
db.myCollection.remove({name: "navindu"});

10. Removing a collection
db.myCollection.remove({});




Some  Funtions















Connecting method With Database and server



Image result for mongodb

Thursday, July 4, 2019

            Nodejs                                    
 

↦Node.js is an open-source server environment
↦Node.js allows you to run JavaScript on the server.

Its help to the user to connect the server and Database  likely it works as an intermediate

 A lot of youth aspires to be a most sought after software developer and so goes on to learn the required programming skills to match to the current market and industrial needs. They have broadly two choices to make
To Be A Front-End Developer
To Be A Back-end Developer.

Node js is a backend develop language. if you want to become a Back-end Developer so you have to study this. It's really hard but if you study hard you will get it soon.

This server-side application directly interacts with the database via an application programming interface (API), which pulls, saves, or changes data.
 Node.js which is the most sought after backend scripting language in the current technology market and is becoming the preferred language options for back-end programmers.

Node.js an asynchronous event-driven JavaScript runtime, which is designed to build scalable network applications. It can handle many concurrent connections at a time, where when connection request are made concurrently for each connection a callback is fired. If there is no task to be performed Node will go to sleep.

History of Node.js

Node.js was first conceived in 2009 by Ryan Dahl and was developed and maintained by Ryan which then got sponsored and supported by Joyent. Dahl was not happy the way Apache Http server used to handle a lot of concurrent connections and the way code was being created which either blocked the entire process or implied multiple execution stacks in the case of simultaneous connections. This leads him to create a Node.js project which he went on to demonstrate at the inaugural European JSConf on November 8, 2009. He used Google Google’s V8 JavaScript engine, an event loop, and a low-level I/O API in his project which won lots of hearts and a standing ovation.

Here is how Node.js handles a file request:


  1. Sends the task to the computer's file system.
  2. Ready to handle the next request.
  3. When the file system has opened and read the file, the server returns the content to the client.
What Can Node.js Do?

  1. Node.js can generate dynamic page content
  2. Node.js can create, open, read, write, delete, and close files on the server
  3. Node.js can collect form data
  4. Node.js can add, delete, modify data in your database.

How to install Node.js on ubuntu

Step #1:
sudo apt-get update

Step #2:
sudo apt-get install build-essential libssl-dev

Step #3:
sudo curl https://raw.githubusercontent.com/creationix/nvm/v0.33.11/install.sh | bash

Step #4 (if no curl):
sudo apt-get install curl
https://raw.githubusercontent.com/creationix/nvm/v0.33.11/install.sh | bash

Step #4:
source ~/.profile

Step #5:
nvm --version

Step #6:
nvm install 6.16.0

Step #7:
nvm ls

Step #8:
node --version

Step #9:
npm --version

to install nodemon
:--npm install -g nodemon


                            


Basic commands  for the Nodejs

exports.log =               {
    console: function(msg)  {
        console.log(msg);  },
    file: function(msg)     {
        // log to file here }
                            }
to export module for define by user

some modules:
http               :   http includes classes, methods  to create Node.js http server.
url                 :   url module includes methods for URL resolution and parsing.
querystring   :  querystring module includes methods to deal with query string.
path              :   path module includes methods to deal with file paths.
fs                  :     fs module includes classes, methods, and events to work with file I/O.
util                :   util module includes utility functions useful for programmers.


to create server:

var http = require('http');         // 1 - Import Node.js core module

var server = http.createServer(function (req, res) { } );   
 // 2 - creating server //handle incomming requests here..

Url module:

var url = require('url');
var adr = 'http://localhost:8080/default.htm?year=2017&month=february';
var q = url.parse(adr, true);

console.log(q.host);             //returns 'localhost:8080'
console.log(q.pathname);    //returns '/default.htm'
console.log(q.search);         //returns '?year=2017&month=february'

var qdata = q.query;            //returns an object: { year: 2017, month: 'february' }

console.log(qdata.month); //returns 'february'



Express

In this tutorial, we will also have a look at the express framework. This framework is built in such a way that it acts as a minimal an...