Basic of nodejs
node js
What is nodejs
It is event driven architecture, lightweight and efficient way of connecting server from FE, non blocking I/O it doesnt have any document, window, instead it has process, global ,
Some commands
process.exit()→ exits the current node
node → runs the node
global.Object → runs whole object
Non blocking function uses calbback function so it takes less time
running a node js application with file
node hello.js
output - ‘Welcome to nodejs’
Note: Below are synchronous files
creating a file
const fs = require('fs'); → require a file syatem to write in txt
fs.writeFileSync('notes.txt', 'My Name is Sujeendra'); → writing a text in .txt
output→ My Name is Sujeendra
appending a file
fs.appendFileSync(‘notes.txt’, I’m a Software Engineer’);
output→ My Name is Sujeendra I’m A Software Engineer
Getting a file from one file to another
util.js
console.log('utils.js');
const name = 'Sujeendra';
module.exports = name;
multiple
console.log('utils.js');
const name = 'Sujeendra';
const add = function (a, b) {
return a + b;
}
module.exports = { name, add };
const name = require('./utils');
const validator = require('validator');
console.log(name);
output : utils.js
Sujeendra
multiple
const { name, add } = require('./utils');
const sum = add(4, -2);
console.log(name);
console.log(sum);
console.log(validator.isEmail('ht.com'));
console.log(validator.isURL('ht://www.google'));
console.log(process.argv);
console.log(process.argv[2]);
output : utils.js
note - using nodemon we can run the application no need to run node <file> to start
installation -- bun i nodemon
Sujeendra
2
false
false
[
'C:\\Program Files\\nodejs\\node.exe', -- which folder
'C:\\Users\\DLT\\Desktop\\node\\notes-app\\app.js', --which file I exceuted
'Sujeendra' - name
]
Sujeendra
const command = process.argv[2];
if (command === 'add') {
console.log('Adding note!');
} else if (command === 'remove') {
console.log('Removing note');
}
note : node app.js add --title="This is my title"
output:
[
'C:\\Program Files\\nodejs\\node.exe',
'C:\\Users\\DLT\\Desktop\\node\\notes-app\\app.js',
'add',
'--title=This is my title'
]
Adding note!
jsut console.log(yargs.argv);
yargs- used to crate a own command line in node
output:{ _: []--func, '$0'--props: 'app.js' }
note:node app.js add --title="Things I buy"
o/p:{ _: [ 'add' ], title: 'Things I buy', '$0': 'app.js' }
creating a yargs command ---
yargs.command({
command: "read",
describe: "Read a note",
handler: function () {
console.log('Reading a note!');
},
});
i/p-- node app.js read
o/p--[
'C:\\Program Files\\nodejs\\node.exe',
'C:\\Users\\DLT\\Desktop\\node\\notes-app\\app.js',
'read'
]
Reading a note!
aegumanet parsing with yargs
{ _: [ 'read' ], '$0': 'app.js' }yargs.command({
command: "add",
describe: "Add a new note",
builder: {
title: {
describe: "Note title",
demandOption: true,
type: "string",
},
},
handler: function (argv) {
console.log('Title:' + argv.title);
},
});
node app.js add --title="Shopping list" --body="Beautiful"
output:'--title=Shopping list' --body="Beautiful"
const dataBuffer = fs.readFileSync('1-json.json'); -- for reading file
const dataJSON = dataBuffer.toString(); -- for converting string
const data = JSON.parse(dataJSON); -- getting the object
data.name = 'Gunther'; -- changing the object name
data.age = 54; -- changing the age
const userJSON = JSON.stringify(data); -- converting back to string
fs.writeFileSync('1-json.json', userJSON);]
Debugging - for debugging use debugger
chrome://inspect/#devices - for seeing which are active
note:in call stack always main() function runs first
note: __dirname- gives the current directory with path
__filename- give sthe current filename with path