2. IWT Syllabus:
Module 5:
Node.js
Introduction, Modules in nodejs, HTTP module, File System, URL module, NPM, events, Upload Files,
Email.
JSP
Server Side Programming: Introduction to Java Server Page (JSP), JSP Application Design, JSP objects,
Conditional Processing, Declaring variables and methods, Sharing data between JSP pages, Sharing
Session and Application Data, Database Programming using JDBC, development of java beans in JSP.
Servlet
Introduction to Servlets, Lifecycle, JSDK, Servlet API, Servlet Packages, Introduction to JSF, JSF Basics,
Managed Beans, Navigation, Standard JSF Tags, Data Tables, Conversion and Validation, Event Handling
3. Node.js:
Node.js is an open source server environment.
Node.js allows you to run JavaScript on the server.
Released on 27th May 2009. (11 years ago)
Ryan Dahl
4. Node.js:
console.log('This example is different!');
console.log('The result is displayed in
the Command Line Interface');
Index.js var http = require('http');
http.createServer(function (req, res)
{
res.writeHead(200, {'Content-
Type': 'text/plain'});
res.end('Hello World!');
}).listen(8080);
Index.js
http://localhost:8080/
5. Node.js Modules:
exports.myDateTime = function () {
return Date();
};
var http = require('http');
var dt = require('./time');
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);
Index.js
time.js
7. Node.js Send an Email:
var nodemailer = require('nodemailer');
var transporter =
nodemailer.createTransport({
service: 'gmail',
auth: {
user: 'youremail@gmail.com',
pass: 'yourpassword'
}
});
var mailOptions = {
from: 'youremail@gmail.com',
to: 'myfriend@yahoo.com',
subject: 'Sending Email using Node.js',
text: 'That was easy!'
};
transporter.sendMail(mailOptions,
function(error, info){
if (error) {
console.log(error);
} else {
console.log('Email sent: ' + info.response);
}
});
Index.js
https://www.google.com/settings/security/lesssecureappsnpm install nodemailer
GMAIL Less secure app accessCMD
8. Node.js MySQL Create Database:
var mysql = require('mysql');
var con = mysql.createConnection({
host: "localhost",
user: "yourusername",
password: "yourpassword"
});
con.connect(function(err) {
if (err) throw err;
console.log("Connected!");
con.query("CREATE DATABASE mydb", function (err,
result) {
if (err) throw err;
console.log("Database created");
});
});
Index.js
npm install mysql
CMD
9. Node.js MySQL Create Table:
var mysql = require('mysql');
var con = mysql.createConnection({
host: "localhost",
user: "yourusername",
password: "yourpassword",
database: "mydb"
});
con.connect(function(err) {
if (err) throw err;
console.log("Connected!");
var sql = "CREATE TABLE customers (name VARCHAR(255), address VARCHAR(255))";
con.query(sql, function (err, result) {
if (err) throw err;
console.log("Table created");
});
});
Index.js
10. Node.js MySQL Insert Into:
var mysql = require('mysql');
var con = mysql.createConnection({
host: "localhost",
user: "yourusername",
password: "yourpassword",
database: "mydb"
});
con.connect(function(err) {
if (err) throw err;
console.log("Connected!");
var sql = "INSERT INTO customers (name, address) VALUES ('Company Inc', 'Highway 37')";
con.query(sql, function (err, result) {
if (err) throw err;
console.log("1 record inserted");
});
});
Index.js
11. Node.js MySQL Update:
var mysql = require('mysql');
var con = mysql.createConnection({
host: "localhost",
user: "yourusername",
password: "yourpassword",
database: "mydb"
});
con.connect(function(err) {
if (err) throw err;
var sql = "UPDATE customers SET address = 'Canyon 123' WHERE address = 'Valley 345'";
con.query(sql, function (err, result) {
if (err) throw err;
console.log(result.affectedRows + " record(s) updated");
});
});
Index.js
12. Node.js MySQL Select From:
var mysql = require('mysql');
var con = mysql.createConnection({
host: "localhost",
user: "yourusername",
password: "yourpassword",
database: "mydb"
});
con.connect(function(err) {
if (err) throw err;
con.query("SELECT * FROM customers", function (err, result, fields) {
if (err) throw err;
console.log(result);
});
});
Index.js
13. Node.js MySQL Where:
var mysql = require('mysql');
var con = mysql.createConnection({
host: "localhost",
user: "yourusername",
password: "yourpassword",
database: "mydb"
});
con.connect(function(err) {
if (err) throw err;
con.query("SELECT * FROM customers WHERE address = 'Park Lane 38'", function (err,
result) {
if (err) throw err;
console.log(result);
});
});
Index.js
14. Node.js MySQL Order By:
var mysql = require('mysql');
var con = mysql.createConnection({
host: "localhost",
user: "yourusername",
password: "yourpassword",
database: "mydb"
});
con.connect(function(err) {
if (err) throw err;
con.query("SELECT * FROM customers ORDER BY name", function (err, result) {
if (err) throw err;
console.log(result);
});
});
Index.js
15. Node.js MySQL Delete:
var mysql = require('mysql');
var con = mysql.createConnection({
host: "localhost",
user: "yourusername",
password: "yourpassword",
database: "mydb"
});
con.connect(function(err) {
if (err) throw err;
var sql = "DELETE FROM customers WHERE address = 'Mountain 21'";
con.query(sql, function (err, result) {
if (err) throw err;
console.log("Number of records deleted: " + result.affectedRows);
});
});
Index.js
16. Node.js MySQL Drop Table:
var mysql = require('mysql');
var con = mysql.createConnection({
host: "localhost",
user: "yourusername",
password: "yourpassword",
database: "mydb"
});
con.connect(function(err) {
if (err) throw err;
var sql = "DROP TABLE customers";
con.query(sql, function (err, result) {
if (err) throw err;
console.log("Table deleted");
});
});
Index.js
17. Model Questions:
1. HTML (Hyper Text Markup Language) has language elements,
which permit certain actions other than describing the structure
of the Web document. Which one of the following actions is NOT
supported by pure HTML (without any server or client-side
scripting) pages?
(a) Embed Web objects from different sites into the same page
(b) Refresh the page automatically after a specified interval.
(c) Automatically redirect to another page upon download.
(d) Display the client time as part of the page.
(GATE 2011: 1 Mark)
Solution:
<OBJECT> … </OBJECT> tag is used to
embed web objects.
<META HTTP-EQUIV="Refresh" CONTENT="5">
is used to refresh page after every 5 seconds.
<META HTTP-EQUIV="Refresh" CONTENT="0;
URL=another-page.html"> is used to redirect.
But for displaying the client time, there is no
tag available.
18. Model Questions: (Cont.)
2. To change the text colour in HTML we use?
(a) Color:
(b) latest—text—color=
(c) modifytextcolor:
(d) newcolor:
3. Which of the following is used to specify border type
(solid or dotted or double line etc.)?
(a) Border-width
(b) Border-attrib
(c) Border-style
(d) Border-layout