├── app.js ├── .gitignore ├── built-in-modules ├── content │ ├── first.txt │ ├── second.txt │ ├── result-async.txt │ ├── result-sync.txt │ ├── result-mind-grenade.txt │ └── big-text.txt ├── path.js ├── file-system.js ├── os.js ├── http.js └── fs-async.js ├── utils.js ├── loadash.js ├── request-event.js ├── create-big-text-file.js ├── event-loop-examples ├── setTimeout.js ├── setInterval.js ├── readFile.js └── server.js ├── names.js ├── external-modules.js ├── package.json ├── streams.js ├── event-emitter.js ├── http-streams.js ├── express-snippets └── http-headers.js └── async-patterns └── fs-async-await.js /app.js: -------------------------------------------------------------------------------- 1 | //3:51:41 -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | /node_modules -------------------------------------------------------------------------------- /built-in-modules/content/first.txt: -------------------------------------------------------------------------------- 1 | Hello This is a first text. -------------------------------------------------------------------------------- /built-in-modules/content/second.txt: -------------------------------------------------------------------------------- 1 | Hello This is a second text. -------------------------------------------------------------------------------- /utils.js: -------------------------------------------------------------------------------- 1 | const greeting =(name)=>{ 2 | console.log(`hey! ${name}`); 3 | } 4 | module.exports=greeting; -------------------------------------------------------------------------------- /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-sync.txt: -------------------------------------------------------------------------------- 1 | Append here: Here is the result : Hello This is a first text., Hello This is a second text. -------------------------------------------------------------------------------- /loadash.js: -------------------------------------------------------------------------------- 1 | const l = require("lodash"); 2 | const items = [1, [2, [3, [4]]]]; 3 | const newItems=l.flattenDeep(items); 4 | console.log(newItems); 5 | -------------------------------------------------------------------------------- /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); -------------------------------------------------------------------------------- /request-event.js: -------------------------------------------------------------------------------- 1 | const http = require('http'); 2 | const server=http.createServer(); 3 | 4 | server.on('request',((req,res)=>{ 5 | res.end('Welcome') 6 | })) 7 | 8 | server.listen(5000) -------------------------------------------------------------------------------- /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-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 -------------------------------------------------------------------------------- /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 -------------------------------------------------------------------------------- /names.js: -------------------------------------------------------------------------------- 1 | module.exports.items=['item1','item2']; 2 | const person ={ 3 | name:'Ahmed' 4 | } 5 | 6 | const ali = 'Ali'; 7 | const tahir = 'Tahir'; 8 | const bilal = 'Bilal' 9 | 10 | module.exports.myPerson=person; 11 | module.exports={ali,bilal} -------------------------------------------------------------------------------- /external-modules.js: -------------------------------------------------------------------------------- 1 | // console.log(module); 2 | const names = require('./names') 3 | const greeting=require('./utils') 4 | // console.log(greeting); 5 | let omer = 'omer' 6 | const data = require('./names') 7 | console.log(data); 8 | // greeting(names.bilal); 9 | // greeting(names.ali); -------------------------------------------------------------------------------- /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/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); -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "node-and-express-js", 3 | "version": "1.0.0", 4 | "description": "", 5 | "main": "app.js", 6 | "scripts": { 7 | "test": "echo \"Error: no test specified\" && exit 1", 8 | "start":"nodemon app.js" 9 | }, 10 | "keywords": [], 11 | "author": "", 12 | "license": "ISC", 13 | "dependencies": { 14 | "lodash": "^4.17.21" 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /streams.js: -------------------------------------------------------------------------------- 1 | const {createReadStream}=require('fs'); 2 | 3 | const stream = createReadStream('./built-in-modules/content/big-text.txt',{highWaterMark:1000,encoding:'utf8'}); 4 | 5 | //default 64kb 6 | //last buffer is a remainder 7 | //highWaterMark - control size 8 | 9 | stream.on('data',(result)=>{ 10 | console.log(result); 11 | }) 12 | 13 | stream.on('error',(err)=>{ 14 | console.log(err); 15 | }) -------------------------------------------------------------------------------- /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-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) -------------------------------------------------------------------------------- /http-streams.js: -------------------------------------------------------------------------------- 1 | let http = require('http'); 2 | let fs = require('fs'); 3 | 4 | http.createServer(function(req,res){ 5 | const fileStream=fs.createReadStream('./built-in-modules/content/big-text.txt','utf8'); 6 | fileStream.on('open',()=>{ 7 | fileStream.pipe(res) 8 | }) 9 | 10 | fileStream.on('error',(err)=>{ 11 | res.end(err); 12 | }) 13 | // const text=fs.readFileSync('./built-in-modules/content/big-text.txt','utf8') 14 | // res.end(text) 15 | 16 | }).listen(5000) -------------------------------------------------------------------------------- /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/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. -------------------------------------------------------------------------------- /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 | }) -------------------------------------------------------------------------------- /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('