Saturday, June 3, 2017
Node.js 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)
Nodejs introduction
π₯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)
π Node.js uses JavaScript on the server
π₯What are the different between Node.js & PHP or ASP
π¦how is 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.
π¦how is 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.
π₯What are the functions in Node.js?
π 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
π₯ Node.js files have extension ".js"
Friday, June 2, 2017
Mongo db Install
π₯ How to install Mongodb
π§If you want to create a database with name <mydb>, then use database statement would be as follows
π₯db.dropDatabase()
π§This will delete the selected database. If you have not selected any database, then it will delete default 'test' database.
πFirst, check the list of available databases by using the command, show dbs.
π§>show dbs
local 0.78125GB
mydb 0.23012GB
test 0.23012GB
πIf you want to delete new database <mydb>, then dropDatabase() command would be as follows
π§>use mydb switched to db mydb
> db.dropDatabase()
>{ "dropped" : "mydb", "ok" : 1 } >
πNow check list of databases.
>show dbs
local 0.78125GB
test 0.23012GB >
π’ Start MongoDB
π sudo service mongodb start
π’ Stop MongoDB
π sudo service mongodb stop
π’ Restart MongoDB
π sudo service mongodb restart
π₯ To use MongoDB run the following command. π mongo
π₯How to create databaseπ§If you want to create a database with name <mydb>, then use database statement would be as follows
>use mydb
switched to db mydb
π§To check your currently selected database, use the command db>db
mydb
π§If you want to check your databases list, use the command show dbs.>show dbs
local 0.78125GB
test 0.23012GB
π§Your created database (mydb) is not present in list. To display database, you need to insert at least one document into it.>db.student.insert({"name":"Abi"})
>show dbs
local 0.78125GB
mydb 0.23012GB
test 0.23012GB
π₯ How to drop Database
Basic syntax of dropDatabase() command is as follows −π₯db.dropDatabase()
π§This will delete the selected database. If you have not selected any database, then it will delete default 'test' database.
πFirst, check the list of available databases by using the command, show dbs.
π§>show dbs
local 0.78125GB
mydb 0.23012GB
test 0.23012GB
πIf you want to delete new database <mydb>, then dropDatabase() command would be as follows
π§>use mydb switched to db mydb
> db.dropDatabase()
>{ "dropped" : "mydb", "ok" : 1 } >
πNow check list of databases.
>show dbs
local 0.78125GB
test 0.23012GB >
MongoDB

π₯ MongoDB is a cross-platform, document oriented database that provides, high performance, high availability, and easy scalability. MongoDB works on concept of collection and document.
π₯ Advantages of MongoDB over RDBMS
- Schema less − MongoDB is a document database in which one
collection holds different documents. Number of fields, content and size
of the document can differ from one document to another.
- Structure of a single object is clear.
- No complex joins.
- Deep query-ability. MongoDB supports dynamic queries on documents
using a document-based query language that's nearly as powerful as SQL.
- Tuning.
- Ease of scale-out − MongoDB is easy to scale.
- Conversion/mapping of application objects to database objects not needed.
- Uses internal memory for storing the (windowed) working set, enabling faster access of data.
π₯ 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
Thursday, June 1, 2017
jQuery Event
π₯ All the different visitor's actions that a web page can respond to are called events.
π₯ An event represents the precise moment when something happens.
Examples:
- moving a mouse over an element
$(document).ready(function(){
$("p").on({
mouseenter: function(){
$(this).css("background-color", "lightgray");
},
mouseleave: function(){
$(this).css("background-color", "lightblue");
},
click: function(){
$(this).css("background-color", "yellow");
}
});
});
</script>
- selecting a radio button
$(document).ready(function(){
$("input").keypress(function(){
$(this).hide();
});
});
</script>
- clicking on an element
- π§<script>
$(document).ready(function(){
$("p").on("click", function(){
$(this).hide();
});
});
</script>
jQuery
π₯ jQuery is a JavaScript Library.
π₯ jQuery greatly simplifies JavaScript programming.
π₯ The purpose of jQuery is to make it much easier to use JavaScript on your website.
π¦ jQuery Syntax
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)

π¦ jQuery Selectors
π¦The element Selector
You can select all <p> elements on a page like this:
$("p")
π¦The #id selector
An id should be unique within a page, so you should use the #id selector when you want to find a single, unique element.
$("#test")
π¦ The . class selector
To find elements with a specific class, write a period character, followed by the name of the class:
$(".test")
Subscribe to:
Posts (Atom)



