Tuesday, 12 February 2019

AJAX

Ajax     

Related imageAJAX is the art of exchanging data with a server, and updating parts of a web page - without reloading the whole page


What is AJAX?

AJAX = Asynchronous JavaScript And XML.

In short; AJAX is about loading data in the background and display it on the webpage, without reloading the whole page.

Examples of applications using AJAX: 
Gmail, Google Maps, Youtube, and Facebook tabs.


What About jQuery and AJAX?

jQuery provides several methods for AJAX functionality.

With the jQuery AJAX methods, you can request text, HTML, XML, or JSON from a remote server using both HTTP Get and HTTP Post - And you can load the external data directly into the selected HTML elements of your web page!


jQuery - AJAX load() Method

jQuery load() Method

The jQuery load() method is a simple, but powerful AJAX method.

The load() method loads data from a server and puts the returned data into the selected element.

Syntax:

$(selector).load(URL,data,callback);

The required URL parameter specifies the URL you wish to load.


The optional data parameter specifies a set of querystring key/value pairs to send along with the request.


The optional callback parameter is the name of a function to be executed after the load() method is completed.


The optional callback parameter specifies a callback function to run when the load() method is completed. The callback function can have different parameters:


responseTxt - contains the resulting content if the call succeeds

statusTxt - contains the status of the call

xhr - contains the XMLHttpRequest object

The following example displays an alert box after the load() method completes. If the load() method has succeeded, it displays "External content loaded successfully!", and if it fails it displays an error message:

Example\

$("button").click(function(){
    $("#div1").load("demo_test.txt", function(responseTxt, statusTxt, xhr){
        if(statusTxt == "success")
            alert("External content loaded successfully!");
        if(statusTxt == "error")
            alert("Error: " + xhr.status + ": " + xhr.statusText);
    });
});


jQuery - AJAX get() and post() Methods

The jQuery get() and post() methods are used to request data from the server with an HTTP GET or POST request.

HTTP Request: GET vs. POST

Two commonly used methods for a request-response between a client and server are: GET and POST.

GET - Requests data from a specified resource
POST - Submits data to be processed to a specified resource

GET is basically used for just getting (retrieving) some data from the server. Note: The GET method may return cached data.

POST can also be used to get some data from the server. However, the POST method NEVER caches data, and is often used to send data along with the request.

To learn more about GET and POST, and the differences between the two methods, please read our HTTP Methods GET vs POST chapter.

jQuery $.get() Method

The $.get() method requests data from the server with an HTTP GET request.

Syntax:

$.get(URL,callback);

The required URL parameter specifies the URL you wish to request.

The optional callback parameter is the name of a function to be executed if the request succeeds.

The following example uses the $.get() method to retrieve data from a file on the server:

Example

$("button").click(function(){
    $.get("demo_test.asp", function(data, status){
        alert("Data: " + data + "\nStatus: " + status);
    });
});


The first parameter of $.get() is the URL we wish to request ("demo_test.asp").

The second parameter is a callback function. The first callback parameter holds the content of the page requested, and the second callback parameter holds the status of the request.


jQuery $.post() Method

The $.post() method requests data from the server using an HTTP POST request.

Syntax:

$.post(URL,data,callback);

The required URL parameter specifies the URL you wish to request.

The optional data parameter specifies some data to send along with the request.

The optional callback parameter is the name of a function to be executed if the request succeeds.

The following example uses the $.post() method to send some data along with the request:

Example

$("button").click(function(){
    $.post("demo_test_post.asp",
    {
        name: "Donald Duck",
        city: "Duckburg"
    },
    function(data, status){
        alert("Data: " + data + "\nStatus: " + status);
    });
});



The first parameter of $.post() is the URL we wish to request ("demo_test_post.asp").

Then we pass in some data to send along with the request (name and city).

The ASP script in "demo_test_post.asp" reads the parameters, processes them, and returns a result.

The third parameter is a callback function. The first callback parameter holds the content of the page requested, and the second callback parameter holds the status of the request.

Monday, 28 January 2019

nodejs modules





What is Modules?
A module encapsulates related code into a single unit of code. When creating a module, this can be interpreted as moving all related functions into a file.


How to create Module
Create a module that returns the current date and time:



      exports.myDateTime = function () {
    return Date();
}; 
  
Save the code above in a file called "firstmodule.js"
Now we  can include and use the module in any of our Node.js files.
var http = require('http');
var dt = require('./myfirstmodule');
http.createServer(function (req, res) {
    res.writeHead(200, {'Content-Type': 'text/html'});
    res.write("The date and time is currently: " + dt.myDateTime());
    res.end();
}).listen(8080);
  Save the code above in a file called "demo_module.js", and initiate the file

  Initiate demo_module.js in Terminal
      C:\Users\Your Name> node demo_module.js 
  If we want result,We will use this path http://localhost:8080

 Node.js HTTP Module
      This allows Node.js to transfer data over the Hyper Text Transfer Protocol (HTTP).

      var http = require('http');

//create a server object:
http.createServer(function (req, res) {
  res.write('Hello World!'); //write a response to the client
  res.end(); //end the response
}).listen(8080); //the server object listens on port 8080

     Save the code above in a file called "demo_http.js" 
     Then use below code in terminal
     C:\Users\Your Name>node demo_http.js
     Then use port to open browser
                out put:
                           
                       Hello World!

 Node.js URL Module

Create two html files and save them in the same folder as your node.js files.

var http = require('http');
var url = require('url');
var fs = require('fs');

http.createServer(function (req, res) {
  var q = url.parse(req.url, true);
  var filename = "." + q.pathname;
  fs.readFile(filename, function(err, data) {
    if (err) {
      res.writeHead(404, {'Content-Type': 'text/html'});
      return res.end("404 Not Found");
    }
    res.writeHead(200, {'Content-Type': 'text/html'});
    res.write(data);
    return res.end();
  });
}).listen(8080); 

   node demo_fileserver.js 

   http://localhost:8080/(htmlfilename)

Sunday, 20 January 2019

node.js


What is Node.js?

  • Node.js is an open source server framework
  • Node.js is free
  • Node.js runs on various platforms (Windows,
  • Linux, Unix, Mac OS X, etc.)
  • Node.js uses JavaScript on the server


Why Want to use Node.js?


  • A common task for a web server can be to open a file on the server and return the content to the client.
  • Here is how PHP or ASP handles a file request:
  • Sends the task to the computer's file system.
  • Waits while the file system opens and reads the file.
  • Returns the content to the client.
  • Ready to handle the next request.

Here is how Node.js handles a file request:


  • Sends the task to the computer's file system.
  • Ready to handle the next request.
  • When the file system has opened and read the file, the server returns the content to the client.
  • Node.js eliminates the waiting, and simply continues with the next request.
  • Node.js runs single-threaded, non-blocking, asynchronously programming, which is very memory efficient.


What Can Node.js Do?

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


What is a Node.js File?


  • Node.js files contain tasks that will be executed on certain events
  • A typical event is someone trying to access a port on the server
  • Node.js files must be initiated on the server before having any effect
  • Node.js files have extension ".js

Install Node.js via NVM


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

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

What Concepts are in Node.js?


* The following diagram depicts some important parts of Node.js which we will discuss in detail in the subsequent chapters.



5.Where to Use Node.js?
  Following are the areas where Node.js is proving itself as a perfect technology    partner.

I/O bound Applications
Data Streaming Applications
Data Intensive Real-time Applications (DIRT)
JSON APIs based Applications
Single Page Applications

--Node.js  modules--


 1.What is a Module in Node.js?

   * Consider modules to be the same as JavaScript libraries. 
     * A set of functions you want to include in your application.

--Include Modules--

* To include a module, use the require() function with the name of the module:
1.var http = require('http'); 
* Now your application has access to the HTTP module, and is able to create a server:
http.createServer(function (req, res) {
    res.writeHead(200, {'Content-Type''text/html'});
    res.end('Hello World!');
}).listen(8080); 

 

--Create Your Own Modules--

* You can create your own modules, and easily include them in your applications.
* The following example creates a module that returns a date and time object:

exports.myDateTime = function () {
    return Date();
};  

Use the exports keyword to make properties and methods available outside the module file.
Save the code above in a file called "mycreatemodule.js"


var http = require('http');
var dt = require('./mycraetemodule');

             ** Include Your Own Module **

http.createServer(function (req, res) {
    res.writeHead(200, {'Content-Type''text/html'});
    res.write("The date and time are currently: " + dt.myDateTime());
    res.end();
}).listen(8080);
 

better way to build a startup


better way to get a startup!!!


before your startup you want to ask some questions with your idea and then if the question's answer is worth you can start your startup as well as

answering the following questions....


1. Your idea 

2. What problems do you want to solve? 
3. What is your target market? 
4. Who are your customers? 
5. How does it solve the problems? 
6. What features will your product have?


Sunday, 13 January 2019

MongoDB

mongodb


MongoDB is an open-source document database and leading NoSQL database.
It is written in C++.
It is a cross-platform.
Document oriented data based.
that provides, high performance, high availability, and easy scalability.It works on concept of collection
and document.

Basic syntax of use DATABASE statement is as follows −
.use DATABASE_NAME 
use music


Your created database (mydb) is not present in list.
To display database, you need to insert at least one document into it.
db.movie.insert({"name":" songdetails"})


MongoDB db.dropDatabase() command is used to drop a existing database.

db.dropDatabase()

To check the list of available databases by using the command, show dbs.

>show dbs
local      0.78125GB
mydb       0.23012GB
test       0.23012GB


MongoDB db.createCollection(name, options) is used to create collection.

db.createCollection(name, options)

MongoDB's db.collection.drop() is used to drop a collection from the database.

db.COLLECTION_NAME.update(SELECTION_CRITERIA, UPDATED_DATA)

Example:


1) Create a Database called music​.


  • use music

2) Create a collection called songdetails​.


  • db.createCollection("songdetails")


3) Create the above 5 song documents.


db.songdetails.insert({"Song Name":"Thaniye Thananthaniye","Film":"Rhythm","Music Director":"A.R.Rahman","Singer":"Shankar mahadevan"})
WriteResult({ "nInserted" : 1 })
db.songdetails.insert({"Song Name":"Evano Oruvan","Film":"Alai Payuthey","Music Director":"A.R.Rahman","Singer":"Swarnalatha"})
WriteResult({ "nInserted" : 1 })
db.songdetails.insert({"Song Name":"Roja Poonthottam","Film":"Kannukkul Nilavu","Music Director":"Ilaiyaraja","Singer":"Unnikirishnan,Anuradha Sriram"})
WriteResult({ "nInserted" : 1 })
db.songdetails.insert({"Song Name":"Vennilave Vennilave vinnaithandi","Film":"Minsara Kanavu","Music Director":"A.R.Rahman","Singer":"Hariharan,Sadhana Sargam"})
WriteResult({ "nInserted" : 1 })
db.songdetails.insert({"Song Name":"Sollamal Thottu Chellum Thendral","Film":"Dheena","Music Director":"Yuvan Shankar Raja","Singer":"Hariharan"})













4) List all documents created.
  • db.songdetails.find().pretty()



5) List A.R.Rahman’s songs.


  • db.songdetails.find({"Music Director":"A.R.Rahman"})



6) List A.R.Rahman’s songs sung by Unnikrishnan.


  • db.songdetails.find({"Music Director":"Ilaiyaraja","Singer":"Unnikirishnan,Anuradha Sriram"},{"Song Name":1,_id:0})

7) Delete the song which you don’t like.
  • db.songdetails.remove({"Song Name":"Evano Oruvan"})



8) Add new song which is your favourite.
  • db.songdetails.insert({"Song Name":"Kannana Kanne","Film":"Viswasam","Music Director":"D.imman","Singer":"Sid Sriram"})

9) List Songs sung by Sid sriram from Viswasam film.


  • db.songdetails.find({"Film":"Viswasam","Singer":"Sid Sriram"},{"Song Name":1,_id:0})



10)List out the singers’ names in your document.


  • db.songdetails.find({},{"Singer":1,_id:0}).pretty()





example2




1) Create a Database called student
use student


2) Create a collection called ​studentmarks
db.creatCollection(“studentmarks”)

3) Create the documents listed in above table.

db.songdetails.insert({"name":"Malal","maths_marks":45,"english_marks":53,"science_marks":72})
.
.
.

4) Increase the maths marks of Mala by 6 marks







5) List the names of students who got more than 50 marks in Maths Subject.


6)Add a new column(field) for Average for all students.
db.studentmarks.aggregate({$addFields:{“avarage”:””}}).pretty()              

7) Update Marks_Science=75 to Lucky .
db.studentmarks.update({“name”:”Lucky”},{$set:{science_marks”:75}})





8) List the names who got more than 50 marks in all subjects.

db.studentmarks.find({$and:[{"english_marks":{$gt:50}},{"science_marks":{$gt:50}},{"maths_marks":{$gt:50}}]},{"name":1,_id:0})



9) List the names who got less than 50 marks in Maths subject and more than 50 marks in English
db.studentmarks.find({$and:[{"english_marks":{$gt:50}},{"maths_marks":{$lt:50}}]},{"name":1,_id:0})



10) List the names who got less than 40 in both Maths and Science.

db.studentmarks.find({$and:[{"science_marks":{$lt:40}},{"maths_marks":{$lt:40}}]},{"name":1,_id:0})
11) Remove Science column/field for Raam
db.studentmarks.update({"name":"Raam"},{$unset:{"science_marks":88}})


12) Update John’s Math mark as 87 and English mark as 23, if john not available upsert.

db.studentmarks.insert({"name":"John","maths_marks":87,"english_marks":23})

13) Rename the english_marks column/field for John to science_marks

db.studentmarks.update({"name":"John"},{$rename:{"english_marks":"science_marks"}})




14) Remove Kumaran’s document from collection

db.studentmarks.remove({"name":"Kumaran"})


15) Find Kala’s or Aruli’s math_marks and science_marks

db.studentmarks.find({"name":"Aruli"},{"maths_marks":1,"science_marks":1}).pretty()