├── .gitignore ├── app.js ├── async-patterns └── fs-async-await.js ├── built-in-modules ├── content │ ├── big-text.txt │ ├── first.txt │ ├── result-async.txt │ ├── result-mind-grenade.txt │ ├── result-sync.txt │ └── second.txt ├── file-system.js ├── fs-async.js ├── http.js ├── os.js └── path.js ├── create-big-text-file.js ├── event-emitter.js ├── event-loop-examples ├── readFile.js ├── server.js ├── setInterval.js └── setTimeout.js ├── express-snippets └── http-headers.js ├── external-modules.js ├── http-streams.js ├── loadash.js ├── names.js ├── package-lock.json ├── package.json ├── request-event.js ├── streams.js └── utils.js /.gitignore: -------------------------------------------------------------------------------- 1 | /node_modules -------------------------------------------------------------------------------- /app.js: -------------------------------------------------------------------------------- 1 | //3:51:41 -------------------------------------------------------------------------------- /async-patterns/fs-async-await.js: -------------------------------------------------------------------------------- 1 | const { readFile, writeFile } = require("fs").promises; 2 | // const util = require('util'); 3 | 4 | // const readFilePromise=util.promisify(readFile); 5 | // const writeFilePromise=util.promisify(writeFile); 6 | 7 | const start = async () => { 8 | try { 9 | const first = await readFile( 10 | "../built-in-modules/content/first.txt", 11 | "utf8" 12 | ); 13 | const second = await readFile( 14 | "../built-in-modules/content/second.txt", 15 | "utf8" 16 | ); 17 | await writeFile( 18 | "../built-in-modules/content/result-mind-grenade.txt", 19 | `This is AWESOME : ${first} ${second}`, 20 | { flag: "a" } 21 | ); 22 | console.log(first, second); 23 | } catch (error) { 24 | console.log(error); 25 | } 26 | }; 27 | start(); 28 | // const getText = (path) => { 29 | // return new Promise((resolve, reject) => { 30 | // readFile(path, "utf8", (err, data) => { 31 | // if (err) { 32 | // reject(err); 33 | // } else { 34 | // resolve(data); 35 | // } 36 | // }); 37 | // }); 38 | // }; 39 | -------------------------------------------------------------------------------- /built-in-modules/content/big-text.txt: -------------------------------------------------------------------------------- 1 | hello world0 2 | hello world1 3 | hello world2 4 | hello world3 5 | hello world4 6 | hello world5 7 | hello world6 8 | hello world7 9 | hello world8 10 | hello world9 11 | hello world10 12 | hello world11 13 | hello world12 14 | hello world13 15 | hello world14 16 | hello world15 17 | hello world16 18 | hello world17 19 | hello world18 20 | hello world19 21 | hello world20 22 | hello world21 23 | hello world22 24 | hello world23 25 | hello world24 26 | hello world25 27 | hello world26 28 | hello world27 29 | hello world28 30 | hello world29 31 | hello world30 32 | hello world31 33 | hello world32 34 | hello world33 35 | hello world34 36 | hello world35 37 | hello world36 38 | hello world37 39 | hello world38 40 | hello world39 41 | hello world40 42 | hello world41 43 | hello world42 44 | hello world43 45 | hello world44 46 | hello world45 47 | hello world46 48 | hello world47 49 | hello world48 50 | hello world49 51 | hello world50 52 | hello world51 53 | hello world52 54 | hello world53 55 | hello world54 56 | hello world55 57 | hello world56 58 | hello world57 59 | hello world58 60 | hello world59 61 | hello world60 62 | hello world61 63 | hello world62 64 | hello world63 65 | hello world64 66 | hello world65 67 | hello world66 68 | hello world67 69 | hello world68 70 | hello world69 71 | hello world70 72 | hello world71 73 | hello world72 74 | hello world73 75 | hello world74 76 | hello world75 77 | hello world76 78 | hello world77 79 | hello world78 80 | hello world79 81 | hello world80 82 | hello world81 83 | hello world82 84 | hello world83 85 | hello world84 86 | hello world85 87 | hello world86 88 | hello world87 89 | hello world88 90 | hello world89 91 | hello world90 92 | hello world91 93 | hello world92 94 | hello world93 95 | hello world94 96 | hello world95 97 | hello world96 98 | hello world97 99 | hello world98 100 | hello world99 101 | hello world0 102 | hello world1 103 | hello world2 104 | hello world3 105 | hello world4 106 | hello world5 107 | hello world6 108 | hello world7 109 | hello world8 110 | hello world9 111 | hello world10 112 | hello world11 113 | hello world12 114 | hello world13 115 | hello world14 116 | hello world15 117 | hello world16 118 | hello world17 119 | hello world18 120 | hello world19 121 | hello world20 122 | hello world21 123 | hello world22 124 | hello world23 125 | hello world24 126 | hello world25 127 | hello world26 128 | hello world27 129 | hello world28 130 | hello world29 131 | hello world30 132 | hello world31 133 | hello world32 134 | hello world33 135 | hello world34 136 | hello world35 137 | hello world36 138 | hello world37 139 | hello world38 140 | hello world39 141 | hello world40 142 | hello world41 143 | hello world42 144 | hello world43 145 | hello world44 146 | hello world45 147 | hello world46 148 | hello world47 149 | hello world48 150 | hello world49 151 | hello world50 152 | hello world51 153 | hello world52 154 | hello world53 155 | hello world54 156 | hello world55 157 | hello world56 158 | hello world57 159 | hello world58 160 | hello world59 161 | hello world60 162 | hello world61 163 | hello world62 164 | hello world63 165 | hello world64 166 | hello world65 167 | hello world66 168 | hello world67 169 | hello world68 170 | hello world69 171 | hello world70 172 | hello world71 173 | hello world72 174 | hello world73 175 | hello world74 176 | hello world75 177 | hello world76 178 | hello world77 179 | hello world78 180 | hello world79 181 | hello world80 182 | hello world81 183 | hello world82 184 | hello world83 185 | hello world84 186 | hello world85 187 | hello world86 188 | hello world87 189 | hello world88 190 | hello world89 191 | hello world90 192 | hello world91 193 | hello world92 194 | hello world93 195 | hello world94 196 | hello world95 197 | hello world96 198 | hello world97 199 | hello world98 200 | hello world99 201 | hello world0 202 | hello world1 203 | hello world2 204 | hello world3 205 | hello world4 206 | hello world5 207 | hello world6 208 | hello world7 209 | hello world8 210 | hello world9 211 | hello world10 212 | hello world11 213 | hello world12 214 | hello world13 215 | hello world14 216 | hello world15 217 | hello world16 218 | hello world17 219 | hello world18 220 | hello world19 221 | hello world20 222 | hello world21 223 | hello world22 224 | hello world23 225 | hello world24 226 | hello world25 227 | hello world26 228 | hello world27 229 | hello world28 230 | hello world29 231 | hello world30 232 | hello world31 233 | hello world32 234 | hello world33 235 | hello world34 236 | hello world35 237 | hello world36 238 | hello world37 239 | hello world38 240 | hello world39 241 | hello world40 242 | hello world41 243 | hello world42 244 | hello world43 245 | hello world44 246 | hello world45 247 | hello world46 248 | hello world47 249 | hello world48 250 | hello world49 251 | hello world50 252 | hello world51 253 | hello world52 254 | hello world53 255 | hello world54 256 | hello world55 257 | hello world56 258 | hello world57 259 | hello world58 260 | hello world59 261 | hello world60 262 | hello world61 263 | hello world62 264 | hello world63 265 | hello world64 266 | hello world65 267 | hello world66 268 | hello world67 269 | hello world68 270 | hello world69 271 | hello world70 272 | hello world71 273 | hello world72 274 | hello world73 275 | hello world74 276 | hello world75 277 | hello world76 278 | hello world77 279 | hello world78 280 | hello world79 281 | hello world80 282 | hello world81 283 | hello world82 284 | hello world83 285 | hello world84 286 | hello world85 287 | hello world86 288 | hello world87 289 | hello world88 290 | hello world89 291 | hello world90 292 | hello world91 293 | hello world92 294 | hello world93 295 | hello world94 296 | hello world95 297 | hello world96 298 | hello world97 299 | hello world98 300 | hello world99 301 | -------------------------------------------------------------------------------- /built-in-modules/content/first.txt: -------------------------------------------------------------------------------- 1 | Hello This is a first text. -------------------------------------------------------------------------------- /built-in-modules/content/result-async.txt: -------------------------------------------------------------------------------- 1 | Here is the result : Hello This is a first text.,Hello This is a second text. -------------------------------------------------------------------------------- /built-in-modules/content/result-mind-grenade.txt: -------------------------------------------------------------------------------- 1 | This is AWESOME : Hello This is a first text. Hello This is a second text.This is AWESOME : Hello This is a first text. Hello This is a second text.This is AWESOME : Hello This is a first text. Hello This is a second text.This is AWESOME : Hello This is a first text. Hello This is a second text.This is AWESOME : Hello This is a first text. Hello This is a second text.This is AWESOME : Hello This is a first text. Hello This is a second text.This is AWESOME : Hello This is a first text. Hello This is a second text. -------------------------------------------------------------------------------- /built-in-modules/content/result-sync.txt: -------------------------------------------------------------------------------- 1 | Append here: Here is the result : Hello This is a first text., Hello This is a second text. -------------------------------------------------------------------------------- /built-in-modules/content/second.txt: -------------------------------------------------------------------------------- 1 | Hello This is a second text. -------------------------------------------------------------------------------- /built-in-modules/file-system.js: -------------------------------------------------------------------------------- 1 | const {readFileSync, writeFileSync}=require('fs') 2 | const first = readFileSync('./content/first.txt','utf-8') 3 | const second = readFileSync('./content/second.txt','utf8') 4 | console.log(first,second); 5 | 6 | writeFileSync('./content/result-sync.txt',`Here is the result : ${first}, ${second}`,{flag:'a'}) -------------------------------------------------------------------------------- /built-in-modules/fs-async.js: -------------------------------------------------------------------------------- 1 | const { readFile, writeFile } = require("fs"); 2 | console.log('start'); 3 | readFile("./content/first.txt", "utf8", (err, result) => { 4 | if (err) { 5 | console.log(err); 6 | } 7 | const first = result; 8 | readFile("./content/second.txt", "utf8", (err, result) => { 9 | if (err) { 10 | console.log(err); 11 | return; 12 | } 13 | const second = result; 14 | writeFile('./content/result-async.txt',`Here is the result : ${first},${second}`,(err,result)=>{ 15 | if(err){ 16 | console.log(err); 17 | return 18 | } 19 | console.log('done with this task'); 20 | }) 21 | }); 22 | }); 23 | // readFile("./content/first.txt", "utf8", (result, err) => { 24 | // if (err) { 25 | // console.log(err); 26 | // } 27 | // console.log('done with this task'); 28 | 29 | // }) 30 | console.log('starting another task'); -------------------------------------------------------------------------------- /built-in-modules/http.js: -------------------------------------------------------------------------------- 1 | const http = require('http'); 2 | const server = http.createServer((req,res)=>{ 3 | if(req.url==='/'){ 4 | res.end("Welcome back!"); 5 | } 6 | if(req.url==='/contact'){ 7 | res.end("Contact us through : "); 8 | } 9 | if(req.url==='/about'){ 10 | res.end("Here is our short history"); 11 | } 12 | res.end(`
We can not find the page you are looking for
14 | back home`) 15 | 16 | 17 | }) 18 | 19 | server.listen(5000); -------------------------------------------------------------------------------- /built-in-modules/os.js: -------------------------------------------------------------------------------- 1 | const { log } = require('console'); 2 | const os=require('os'); 3 | const user=os.userInfo(); 4 | console.log(user); 5 | console.log(`The system uptime is ${os.uptime()} seconds.`); 6 | const currentOS={ 7 | name: os.type(), 8 | release: os.release(), 9 | totalMem: os.totalmem(), 10 | freeMem: os.freemem(), 11 | } 12 | console.log(currentOS); -------------------------------------------------------------------------------- /built-in-modules/path.js: -------------------------------------------------------------------------------- 1 | const path = require('path') 2 | console.log(path.sep); 3 | 4 | const filePath= path.join('/built-in-modules','os.js') 5 | console.log(filePath); -------------------------------------------------------------------------------- /create-big-text-file.js: -------------------------------------------------------------------------------- 1 | const {writeFileSync}=require('fs') 2 | 3 | for(let i=0; i<100; i++){ 4 | writeFileSync('./built-in-modules/content/big-text.txt',`hello world${i}\n`,{flag:'a'}) 5 | } -------------------------------------------------------------------------------- /event-emitter.js: -------------------------------------------------------------------------------- 1 | const EventEmitter = require('events'); 2 | const customEmitter = new EventEmitter(); 3 | customEmitter.on("response",(name,id)=>{ 4 | console.log(`data received from the user with name: ${name} and id: ${id}`); 5 | }) 6 | customEmitter.on("response",()=>{ 7 | console.log(`some other logic here`); 8 | }) 9 | 10 | //order matters, first listen then will emit the event. 11 | //additional arguments 12 | 13 | customEmitter.emit("response",'ali',23) -------------------------------------------------------------------------------- /event-loop-examples/readFile.js: -------------------------------------------------------------------------------- 1 | const {readFile} = require('fs'); 2 | 3 | console.log("started first week"); 4 | 5 | //Check File Path: 6 | 7 | readFile('../built-in-modules/content/first.txt','utf8',(err,result)=>{ 8 | if(err){ 9 | console.log(err); 10 | return 11 | } 12 | console.log(result); 13 | console.log('completed first task'); 14 | }) 15 | 16 | console.log('starting next task.'); -------------------------------------------------------------------------------- /event-loop-examples/server.js: -------------------------------------------------------------------------------- 1 | const http = require("http"); 2 | const server = http.createServer((req, res) => { 3 | if (req.url === "/") { 4 | res.end("Home Page."); 5 | return 6 | } 7 | if (req.url === "/about") { 8 | //BLOCKING CODE!!!! 9 | for(let i=0; i<10; i++){ 10 | for(let j=0; j<1000; j++){ 11 | console.log(`${i} ${j}`); 12 | } 13 | } 14 | res.end("About Page."); 15 | return 16 | } 17 | res.end('Error Page.') 18 | }); 19 | 20 | server.listen(5000,()=>{ 21 | console.log('server is listening on port 5000...'); 22 | }) -------------------------------------------------------------------------------- /event-loop-examples/setInterval.js: -------------------------------------------------------------------------------- 1 | setInterval(()=>{ 2 | console.log('js is synchronous.'); 3 | },2000) 4 | 5 | console.log('I will run fast'); 6 | //process stays alive unless: 7 | //Kill process CTRL+C 8 | //unexpected error -------------------------------------------------------------------------------- /event-loop-examples/setTimeout.js: -------------------------------------------------------------------------------- 1 | //starting system process 2 | console.log('first'); 3 | 4 | setTimeout(()=>{ 5 | console.log('second'); 6 | },0) 7 | 8 | console.log('third'); 9 | //completed and exiting -------------------------------------------------------------------------------- /express-snippets/http-headers.js: -------------------------------------------------------------------------------- 1 | const http = require("http"); 2 | //4.32.23 3 | const server = http.createServer((req, res) => { 4 | // console.log(req); 5 | // console.log(res); 6 | const url = req.url; 7 | if (url === "/") { 8 | res.writeHead(200, { "content-type": "text/html" }); 9 | res.write('