Node Js
Node Js
const fs = require('fs');
e) What is REPL?
REPL stands for Read-Eval-Print Loop. It is a simple, interactive shell that processes
Node.js expressions.
host: 'localhost',
user: 'root',
password: '',
database: 'school'
});
con.connect(err => {
if (err) throw err;
let sql = "UPDATE student SET marks = 90 WHERE rno = 1";
con.query(sql, (err, result) => {
if (err) throw err;
console.log("Records updated: " + result.affectedRows);
});
});
c) Purpose of module.exports in Node.js:
It is used to export functions, objects, or variables from a module so they can be used
in other files.
d) AngularJS vs NodeJS:
// Synchronous
fs.writeFileSync('sync.txt', 'Hello sync');
// Asynchronous
fs.writeFile('async.txt', 'Hello async', (err) => {
if (err) throw err;
console.log('Written asynchronously');
});
a) Create Customer DB and Account Table:
const mysql = require('mysql');
const con = mysql.createConnection({
host: "localhost",
user: "root",
password: ""
});
con.connect(err => {
if (err) throw err;
con.query("CREATE DATABASE customerDB", (err) => {
if (err) throw err;
con.changeUser({database: 'customerDB'}, () => {
const sql = "CREATE TABLE account (cid INT, name VARCHAR(255), balance
FLOAT)";
con.query(sql, (err) => {
if (err) throw err;
console.log("Table created");
});
});
});
});
b) Buffer Operations:
const fs = require('fs');
const word = "Node";
fs.readFile('file.txt', 'utf8', (err, data) => {
if (err) throw err;
if (data.includes(word)) {
console.log(`${word} found`);
} else {
console.log(`${word} not found`);
}
});
e) Dependencies vs devDependencies:
b) Blocking vs Non-blocking:
Blocking Non-blocking
Let me know if you want these formatted into a PDF or used in a practice test!
set 2
Here's the complete solution for the Node.js question paper (S.Y. B.B.A. C.A.
Semester IV):
fs.unlink('filename', callback);
exports.sayHello = function() {
return "Hello from Module!";
};
In main.js:
let myModule = require('./myModule');
console.log(myModule.sayHello());
2. Example:
const fs = require('fs');
fs.writeFileSync('data.txt', 'This is written synchronously');
d) Write a code for selecting all records from the “employee” table.
const fs = require('fs');
const prompt = require('prompt-sync')();
let file1 = prompt('Enter first file: ');
let file2 = prompt('Enter second file: ');
let data1 = fs.readFileSync(file1, 'utf8');
let data2 = fs.readFileSync(file2, 'utf8');
fs.writeFileSync('combined.txt', data1 + data2);
console.log('Files combined!');