node JS
node JS
js
https://www.youtube.com/watch?v=nkB3_dIDC08
Java Script : Front end validation language
-Purpose: It was used for validating the
input data
-JS was running inside the browser
Browsers: were executing Javascript code
- Browsers were using JS V8 Engine to
convert JS to Machine Language for execution
Around 2009, they came up with this idea.
- Why can’t we use JS outside the browser,
run it like a server side script
- To run this like a server side script, need a
lot of libraries.
i) JS should be able to communicate with
the OS
ii) JS should be able to communicate with
the networks
iii) JS should be able to access the files
iv) should be able to communicate with the
NodeJS: JS + modules +
packages + JS V8 Engine : servers
enable JS to act like server v) Should also be working as a server
side script
We need a lot of libraries which are said to be
modules
NodeJS
• Its an open-source runtime
environment to run Javascript
code as a client-side script.
• To make JS run outside the
browser, we provide JS with V8
engine
• JS + modules + packages +
JS V8 Engine : enable us to
run JS outside the browser
• We call this environment as
NodeJS: JS + modules +
packages + JS V8 Engine : nodeJS
enable JS to act like server
side script
Why NodeJS?
• Although we have learned various server-side scripting
languages such as PHP, ASP, and JSP, what makes
Node.js a superior choice?
• nodeJS is more scalable and faster (not gonna
concentrate on why is scalable and faster)
• It is used by popular organizations across the globe
3. Type code .
Vscode gets opened automatically
This will tell you the path where global packages are
installed. If it's not set to a path you expect, you can set it to
your desired path using: npm config set prefix "C:\Users\
svlek\AppData\Roaming\npm"
Execute first.js
Install nodemon: After ensuring the global path is set
correctly, try installing nodemon globally again: npm
install -g nodemon
Execute first.js
Run: nodemon first.js
Execute first.js
Temporarily Change Execution Policy:
1.Open a PowerShell window as an administrator.
2.Run the following command to temporarily change the execution
policy for the current session: Set-ExecutionPolicy -
ExecutionPolicy Bypass -Scope Process
3.This allows you to run scripts in the current PowerShell session. The
policy will revert back to its previous state when you close the
Execute first.js
Permanently Change Execution Policy:
1.Open a PowerShell window as an administrator.
2.Run the following command to permanently change the
execution policy:
Set-ExecutionPolicy RemoteSigned -Scope
CurrentUser
Execute first.js
Go back to nodeJS
Run: nodemon first.js
Execute first.js
In first.js, do some modification as :
console.log("hope this works!!!");
• Learnt
• Why we need nodejs
• Installation of nodejs
main().catch(console.error);
console.log("Databases:");
databasesList.databases.forEach(db => console.log(` - ${db.name}`));
};
Exercise 1 Establishing connection with the database and
fetching all the databases : first_dbconnection.js(cont…)
output
PS D:\VIT\23-24\Winter\IP-Web Programming\KL ppts\Module 7\nodejs>
nodemon first_dbconnection.js
[nodemon] 3.1.0
[nodemon] to restart at any time, enter `rs`
[nodemon] watching path(s): *.*
[nodemon] watching extensions: js,mjs,cjs,json
[nodemon] starting `node first_dbconnection.js`
Databases:
- admin
- config
- local
- satishdb
[nodemon] clean exit - waiting for changes before restart
Insert a record in the collection by named vscode in
database vitdb : first_ins.js
const {MongoClient} = require('mongodb’);
}
Insert a record in the collection by named vscode in
database vitdb : first_ins.js (cont…)
catch (e) {
console.error(e);
} finally {
await client.close();
}
}
main().catch(console.error);
}
Run it in Vscode First ….
Insert a record in the collection by named vscode in
database vitdb : first_ins.js (cont…)
output
PS F:\Winter Semester 2022-23\codedemo> node first.js
New listing created with the following id: 6432ba80e40381b7aa9547c9
PS F:\Winter Semester 2022-23\codedemo>
if (result) {
console.log(`Found a listing in the collection with the name '$
{username}':`);
console.log(result);
} else {
console.log(`No listings found with the name '${nameOfListing}'`);
}
}
search for a document (search for document with the name
attribute as Satish) : first_search.js (cont..)
else {
console.log(`No listings found`);
}
name: Satish
summary: Satish is teaching MongoDB
name: Satish
summary: another record
Reading all Records from a
collection in Mongodb
Reading all Records from a collection in Mongodb :
first_readall.js
else {
console.log(‘No listings found’);
}
Output in Database
> db.vscode.find();
{ "_id" : ObjectId("64327ce9bb103b2a3a353b7e"), "name" : "Mathew",
"course" : "testing db connection", "summary" : "Updated summary" }
{ "_id" : ObjectId("6432b9c59061f67fa5c2d6f5"), "name" : "Ram", "summary" :
"Testing Inserting a Record" }
{ "_id" : ObjectId("6432ba80e40381b7aa9547c9"), "name" : "Satish", "summary"
: "Satish is teaching MongoDB" }
{ "_id" : ObjectId("6432bf0a86b0db063f0ec8b3"), "name" : "Satish", "summary"
: "another record" }
Deleting a document from the
Collection
Deleting a document from the Collection: : first_del.js
} catch (e) {
console.error(e);
} finally {
await client.close();
}
}
Deleting a document from the Collection: : first_del.js (cont..)
main().catch(console.error);
Database Output
> db.vscode.find();
{ "_id" : ObjectId("64327ce9bb103b2a3a353b7e"), "name" : "Mathew",
"course" : "testing db connection", "summary" : "Updated summary" }
{ "_id" : ObjectId("6432ba80e40381b7aa9547c9"), "name" : "Satish", "summary"
: "Satish is teaching MongoDB" }
{ "_id" : ObjectId("6432bf0a86b0db063f0ec8b3"), "name" : "Satish", "summary"
: "another record" }
>