SYBBA (CA) Nodejs PDF
SYBBA (CA) Nodejs PDF
Subject:Nodejs
Npm init –y
Npm I uppercase
const up = require('upper-case')
console.log(up.upperCase("Hello world"));
QB)
host : 'localhost',
user : 'root',
password : '',
database:'awani2'
});
con.connect(function(err) {
if (err) throw err;
console.log("Connected!");
var sql = "CREATE TABLE customers (rollno VARCHAR(255), name VARCHAR(5))";
con.query(sql, function (err, result) {
if (err) throw err;
console.log("Table created");
});
});
Prof.D.G.Dhumal
Anantrao Thopte college,Bhor
Question 2
Code for
var rl = require('readline').createInterface({
input: process.stdin,
output: process.stdout
});
var ans=factorial(x);
rl.close();
});
……………………………………………………………………………………………………………………………………………………
if (x === 0)
return 1;
return x * factorial(x-1);
}
Prof.D.G.Dhumal
Anantrao Thopte college,Bhor
Circleexp.js
Prof.D.G.Dhumal
Anantrao Thopte college,Bhor
Slip 5 A
Prof.D.G.Dhumal
Anantrao Thopte college,Bhor
Q5B
con.connect(function(err) {
if (err) throw err;
console.log("Connected!");
//data insert
var data1 = "INSERT INTO customer2 (name, address) VALUES ('Samarth Mali',
'Nandurbar')";
var data2 = "INSERT INTO customer2 (name, address) VALUES ('Pratik Swami',
'Pune')";
var data3 = "INSERT INTO customer2 (name, address) VALUES ('Bhushan Deshmukh',
'Akola')";
con.query(data1, function (err, result) {
if (err) throw err;
console.log( "no. 1 record inserted");
});
con.query(data2, function (err, result) {
if (err) throw err;
console.log( "no. 2 record inserted");
});
con.query(data3, function (err, result) {
if (err) throw err;
console.log( "no. 3 record inserted");
});
//delete data
var row = "DELETE FROM customer2 WHERE address = 'Pune'";
con.query(row, function (err, data) {
if (err) throw err;
console.log("Number of records deleted: " + data.affectedRows);
});
Prof.D.G.Dhumal
});
Anantrao Thopte college,Bhor
Slip 6 A
Prof.D.G.Dhumal
Anantrao Thopte college,Bhor
Slip 7 _A
var fs = require('fs');
var rl = require('readline').createInterface({
input: process.stdin,
output: process.stdout
});
fs.appendFile('1.txt', '2.txt',function(err){
if(err) throw err;
});
Slip7 _B
con.connect(function(err) {
if (err) throw err;
console.log("Connected!");
});
Slip8_A
app.get('/',function(req,res){
res.sendFile(__dirname + "/index1.html");
});
app.post('/upload',function(req,res){
upload(req,res,function(err) {
if(err) {
return res.end("Error uploading file.");
}
res.end("File is uploaded successfully!");
});
});
app.listen(2000,function(){
console.log("Server is running on port 2000");
});
<html>
<head>
<title>File upload in Node.js </title>
<script
src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script
src="http://cdnjs.cloudflare.com/ajax/libs/jquery.form/3.51/jquery.form.min.js"><
/script>
<script>
$(document).ready(function() {
$('#uploadForm').submit(function() {
$("#status").empty().text("File is uploading...");
Prof.D.G.Dhumal
Anantrao Thopte college,Bhor
$(this).ajaxSubmit({
error: function(xhr) {
status('Error: ' + xhr.status);
},
success: function(response) {
console.log(response)
$("#status").empty().text(response);
}
});
return false;
});
});
</script>
</head>
<body>
<h1>Express.js File Upload</h1>
<form id="uploadForm" enctype="multipart/form-data" action="/upload"
method="post">
<input type="file" name="myfile" /><br/><br/>
<input type="submit" value="Upload Image" name="submit"><br/><br/>
<span id="status"></span>
</form>
</body>
</html>
Slip 10
var options = {
url: 'https://i.ytimg.com/vi/6QcuNl1yXrw/maxresdefault.jpg',
dest: 'download/' // will be saved to /path/to/dest/image.jpg
}
download.image(options)
.then(({ filename }) => {
console.log('Saved to', filename) // saved to /path/to/dest/image.jpg
Prof.D.G.Dhumal
Anantrao Thopte college,Bhor
})
Slip no 11
<!DOCTYPE html>
<html>
<head>
<title>form</title>
<style>
h1{text-align:center;
font-size:70px;
color:red;
}
p{
color:blue;
text-align:center;
font-size:20px;
text-decoration:underline}
h2{
text-align:center;
}
</style>
<body bgcolor=pink>
<h1>Anantrao Thopte College</h1>
<center><img src=c.gif height=350 width=350></center>
<p>
Rajgad Dnyanpeeth's<h2>
ANANTRAO THOPTE COLLEGE<br>
Prof.D.G.Dhumal
Anantrao Thopte college,Bhor
BHOR, DIST-PUNE 412206.<br>
Affiliated to Savitribai Phule Pune University, Accredited by NAAC 'A' Grade</p>
</h2>
<a href=demospa.html>Click Here For Registration</a></h2></body></html>
Slip no 12_B
app.get("/",function(req,res){
res.sendFile(__dirname + "/index.html");
})
app.post("/",encoder, function(req,res){
var username = req.body.username;
var password = req.body.password;
con.query("select * from loginuser where user_name = ? and user_pass =
?",[username,password],function(error,results,fields){
if (results.length > 0) {
res.redirect("/loggedine");
} else {
res.redirect("/");
}
res.end();
})
})
app.get("/loggedine",function(req,res){
res.sendFile(__dirname + "/loggedine.html")
})
Prof.D.G.Dhumal
Anantrao Thopte college,Bhor
app.listen(8080);
Slip 13_A.js
var rl = require('readline').createInterface({
input: process.stdin,
output: process.stdout
});
});
Rect.js
return l*b;
Prof.D.G.Dhumal
Anantrao Thopte college,Bhor
};
Slip13_B
con.connect(function(err) {
if (err) throw err;
var sql = "UPDATE student SET Marks = 90 WHERE Roll__num = 1";
con.query(sql, function (err, result,display) {
if (err) throw err;
console.log(result.affectedRows + " record updated");
});
Slip14A
const fs = require("fs");
Prof.D.G.Dhumal
Anantrao Thopte college,Bhor
Slip 15
const fs = require("fs");
Slp 16B
con.connect(function(err) {
if (err) throw err;
var sql = "UPDATE Employee SET Sal = 50000 WHERE Eno = 1";
con.query(sql, function (err, result,display) {
if (err) throw err;
console.log(result.affectedRows + " record updated");
});
Prof.D.G.Dhumal
Anantrao Thopte college,Bhor
Slip17 B
con.connect(function(err) {
if (err) throw err;
console.log("Connected!");
var sql = "CREATE TABLE IF NOT EXISTS Employee (name VARCHAR(255), sal
INT(20))";
con.query(sql, function (err, result) {
if (err) throw err;
console.log("Table created");
});
Prof.D.G.Dhumal
Anantrao Thopte college,Bhor
});
});
Slip22
var fs = require('fs');
filePath = process.argv[2];
File_sam = fs.readFileSync('./sam.txt');
to_string = File_sam.toString();
lines = to_string.split("\n");
console.log('No of lines is : ' + lines.length);
slip28
var fs = require("fs");
var buf = new Buffer(1024);
console.log("Going to open an existing file");
fs.open('input.txt', 'r+', function(err, fd) {
if (err) {
return console.error(err);
}
console.log("File opened successfully!");
console.log("Going to truncate the file after 10 bytes");
// Truncate the opened file.
fs.ftruncate(fd, 10, function(err){
if (err){
console.log(err);
}
console.log("File truncated successfully.");
console.log("Going to read the same file");
fs.read(fd, buf, 0, buf.length, 0, function(err, bytes){if (err){
console.log(err);
}
// Print only read bytes to avoid junk.
if(bytes > 0){
console.log(buf.slice(0, bytes).toString());
}
// Close the opened file.
Prof.D.G.Dhumal
Anantrao Thopte college,Bhor
fs.close(fd, function(err){
if (err){
console.log(err);
}
console.log("File closed successfully.");
});
});
});
});
Slip 29
var rl = require('readline').createInterface({
input: process.stdin,
output: process.stdout
});
});
Prof.D.G.Dhumal
Anantrao Thopte college,Bhor
Prof.D.G.Dhumal
Anantrao Thopte college,Bhor