|
| 1 | +# Recitation #3 |
| 2 | +### 17-356/17-766 |
| 3 | + |
| 4 | +## What is Node.js |
| 5 | +- Open-source, cross-platform JavaScript runtime environment. |
| 6 | +- Event-driven architecture capable of asynchronous (non-blocking) I/O. |
| 7 | +- Suitable for fast, powerful, and scalable web applications. |
| 8 | +- JavaScript and Node.js are the most popular technologies for web development. |
| 9 | + |
| 10 | +## Npm (Node Package Manager) |
| 11 | +- Largest ecosystem of open-source libraries in the world. |
| 12 | +- npm registry - an online database of public and paid-for private packages. |
| 13 | + |
| 14 | +### How to find reliable, secure packages: |
| 15 | +1. Run `npm audit` after installing to check for vulnerabilities. |
| 16 | +2. Look for an active GitHub repository (stars, forks, and issues). |
| 17 | +3. Check `package.json` dependencies: |
| 18 | + ```json |
| 19 | + "dependencies": { |
| 20 | + "axios": "^1.7.9", |
| 21 | + "cra-template": "1.2.0", |
| 22 | + "react": "^18.0.0", |
| 23 | + "react-dom": "^18.0.0", |
| 24 | + "react-scripts": "5.0.1", |
| 25 | + "web-vitals": "^4.2.4" |
| 26 | + } |
| 27 | + ``` |
| 28 | + |
| 29 | +## Simple Web Architecture |
| 30 | +- **Frontend**: React / Vue + RESTful APIs |
| 31 | +- **Backend**: Node.js |
| 32 | +- **Database**: PostgreSQL / MySQL / MongoDB |
| 33 | + |
| 34 | +## Node.js + MongoDB |
| 35 | + |
| 36 | +### What is MongoDB? |
| 37 | +- NoSQL document-based database. |
| 38 | +- Stores data in JSON-like BSON format. |
| 39 | +- Scalable, flexible, and schema-less. |
| 40 | + |
| 41 | +### Schema Example: |
| 42 | +```json |
| 43 | +{ |
| 44 | + "name": "Harry Potter", |
| 45 | + "author": { |
| 46 | + "name": "J.K. Rowling", |
| 47 | + "followers": 789 |
| 48 | + }, |
| 49 | + "views": 123456 |
| 50 | +} |
| 51 | +``` |
| 52 | + |
| 53 | +### Node.js Asynchronous Execution |
| 54 | +```javascript |
| 55 | +function function1() { |
| 56 | + setTimeout(() => { |
| 57 | + console.log('function1'); |
| 58 | + }, 1000); // Delays execution by 1 second |
| 59 | +} |
| 60 | +function function2() { |
| 61 | + console.log('function2'); |
| 62 | +} |
| 63 | +function main() { |
| 64 | + function1(); |
| 65 | + function2(); |
| 66 | +} |
| 67 | +``` |
| 68 | + |
| 69 | +### Output |
| 70 | +``` |
| 71 | +function2 |
| 72 | +function1 |
| 73 | +``` |
| 74 | + |
| 75 | +### Callback Functions |
| 76 | + |
| 77 | +1. Functions passed to another function |
| 78 | +2. Executes the callback after function1 completes |
| 79 | + |
| 80 | +Example |
| 81 | +```javascript |
| 82 | +function function1(callback) { |
| 83 | + setTimeout(() => { |
| 84 | + console.log('function1'); |
| 85 | + callback(); |
| 86 | + }, 1000); |
| 87 | +} |
| 88 | +function function2() { |
| 89 | + console.log('function2'); |
| 90 | +} |
| 91 | +function main() { |
| 92 | + function1(function2); |
| 93 | +} |
| 94 | +``` |
| 95 | + |
| 96 | +### Output |
| 97 | +``` |
| 98 | +function1 |
| 99 | +function2 |
| 100 | +``` |
| 101 | + |
| 102 | +### Promises and Promise Chains |
| 103 | + |
| 104 | +1. Structured callbacks that avoid "callback hell". |
| 105 | +2. Resolves the promise after function1 completes. |
| 106 | + |
| 107 | +Example |
| 108 | +```javascript |
| 109 | +function function1() { |
| 110 | + return new Promise((resolve) => { |
| 111 | + setTimeout(() => { |
| 112 | + console.log('function1'); |
| 113 | + resolve(); |
| 114 | + }, 1000); |
| 115 | + }); |
| 116 | +} |
| 117 | +function function2() { |
| 118 | + console.log('function2'); |
| 119 | +} |
| 120 | +function main() { |
| 121 | + function1().then(function2); // Waits for function1 before running function2 |
| 122 | +} |
| 123 | +``` |
| 124 | + |
| 125 | +Output |
| 126 | +``` |
| 127 | +function1 |
| 128 | +function2 |
| 129 | +``` |
| 130 | + |
| 131 | +### Generators |
| 132 | +1. Functions that can be exited/paused and later renamed |
| 133 | + |
| 134 | +Example |
| 135 | +```javascript |
| 136 | +function* main() { |
| 137 | + yield new Promise((resolve) => { |
| 138 | + setTimeout(() => { |
| 139 | + console.log('function1'); |
| 140 | + resolve(); |
| 141 | + }, 1000); |
| 142 | + }); |
| 143 | + |
| 144 | + console.log('function2'); |
| 145 | +} |
| 146 | +const generator = main(); |
| 147 | +generator.next().value.then(() => generator.next()); |
| 148 | +``` |
| 149 | + |
| 150 | +Output |
| 151 | +``` |
| 152 | +function1 |
| 153 | +function2 |
| 154 | +``` |
| 155 | + |
| 156 | +### Async/Await |
| 157 | +1. Combines generators and promises. |
| 158 | +2. Waits for function1 before running function2. |
| 159 | +3. Functions that use await must be declared as async. |
| 160 | + |
| 161 | +```javascript |
| 162 | +function function1() { |
| 163 | + return new Promise((resolve) => { |
| 164 | + setTimeout(() => { |
| 165 | + console.log('function1'); |
| 166 | + resolve(); |
| 167 | + }, 1000); |
| 168 | + }); |
| 169 | +} |
| 170 | +function function2() { |
| 171 | + console.log('function2'); |
| 172 | +} |
| 173 | +async function main() { |
| 174 | + await function1(); |
| 175 | + function2(); |
| 176 | +} |
| 177 | +``` |
| 178 | + |
| 179 | +``` |
| 180 | +function1 |
| 181 | +function2 |
| 182 | +``` |
0 commit comments