Recitation #3¶
17-356/17-766¶
What is Node.js¶
- Open-source, cross-platform JavaScript runtime environment.
- Event-driven architecture capable of asynchronous (non-blocking) I/O.
- Suitable for fast, powerful, and scalable web applications.
- JavaScript and Node.js are the most popular technologies for web development.
Npm (Node Package Manager)¶
- Largest ecosystem of open-source libraries in the world.
- npm registry - an online database of public and paid-for private packages.
How to find reliable, secure packages:¶
- Run
npm audit
after installing to check for vulnerabilities. - Look for an active GitHub repository (stars, forks, and issues).
- Check
package.json
dependencies:"dependencies": { "axios": "^1.7.9", "cra-template": "1.2.0", "react": "^18.0.0", "react-dom": "^18.0.0", "react-scripts": "5.0.1", "web-vitals": "^4.2.4" }
Simple Web Architecture¶
- Frontend: React / Vue + RESTful APIs
- Backend: Node.js
- Database: PostgreSQL / MySQL / MongoDB
Node.js + MongoDB¶
What is MongoDB?¶
- NoSQL document-based database.
- Stores data in JSON-like BSON format.
- Scalable, flexible, and schema-less.
Schema Example:¶
{
"name": "Harry Potter",
"author": {
"name": "J.K. Rowling",
"followers": 789
},
"views": 123456
}
Node.js Asynchronous Execution¶
function function1() {
setTimeout(() => {
console.log('function1');
}, 1000); // Delays execution by 1 second
}
function function2() {
console.log('function2');
}
function main() {
function1();
function2();
}
Output¶
function2
function1
Callback Functions¶
- Functions passed to another function
- Executes the callback after function1 completes
Example
function function1(callback) {
setTimeout(() => {
console.log('function1');
callback();
}, 1000);
}
function function2() {
console.log('function2');
}
function main() {
function1(function2);
}
Output¶
function1
function2
Promises and Promise Chains¶
- Structured callbacks that avoid "callback hell".
- Resolves the promise after function1 completes.
Example
function function1() {
return new Promise((resolve) => {
setTimeout(() => {
console.log('function1');
resolve();
}, 1000);
});
}
function function2() {
console.log('function2');
}
function main() {
function1().then(function2); // Waits for function1 before running function2
}
Output
function1
function2
Generators¶
- Functions that can be exited/paused and later renamed
Example
function* main() {
yield new Promise((resolve) => {
setTimeout(() => {
console.log('function1');
resolve();
}, 1000);
});
console.log('function2');
}
const generator = main();
generator.next().value.then(() => generator.next());
Output
function1
function2
Async/Await¶
- Combines generators and promises.
- Waits for function1 before running function2.
- Functions that use await must be declared as async.
function function1() {
return new Promise((resolve) => {
setTimeout(() => {
console.log('function1');
resolve();
}, 1000);
});
}
function function2() {
console.log('function2');
}
async function main() {
await function1();
function2();
}
function1
function2