├── README.md ├── core-modules ├── fs │ ├── async.js │ ├── data │ │ ├── first.txt │ │ ├── fourth.txt │ │ ├── second.txt │ │ └── third.txt │ └── sync.js ├── http.js ├── os.js └── path.js ├── event-loop ├── blocking_code.js └── setTimeout.js ├── events ├── http.js └── index.js ├── file-system ├── read.js └── write.js ├── globals ├── globals.js └── timers.js ├── hello-world └── index.js ├── http └── index.js ├── interfacing-with-io └── greeting.js ├── javascript_basics ├── conditionals.js ├── functions.js ├── loops.js └── variables.js ├── modules ├── example1 │ ├── main.js │ └── myModule.js ├── index.js └── math.js ├── promises ├── async_await.js ├── data │ ├── first.txt │ ├── second.txt │ └── third.txt ├── promises.js └── promisify.js ├── repl └── names.js ├── static-file-server ├── index.js └── static │ ├── index.html │ ├── page1.html │ └── page2.html ├── streams ├── bigFile.txt ├── createBigFile.js ├── http.js └── readStream.js └── url └── index.js /README.md: -------------------------------------------------------------------------------- 1 | # Index Course 2 | - hello-world 3 | - modules/ 4 | - core-modules 5 | - file-system 6 | - http 7 | - url 8 | - static file server 9 | -------------------------------------------------------------------------------- /core-modules/fs/async.js: -------------------------------------------------------------------------------- 1 | console.log("start file"); 2 | const { readFile, writeFile } = require("fs"); 3 | 4 | readFile("./data/first.txt", (err, data) => { 5 | if (err) { 6 | console.log(err); 7 | return; 8 | } 9 | 10 | // console.log(data.toString()); 11 | 12 | readFile("./data/second.txt", (err, data) => { 13 | if (err) { 14 | console.log(err); 15 | return; 16 | } 17 | // console.log(data.toString()); 18 | 19 | writeFile( 20 | "./data/fourth.txt", 21 | `this is the fourth file ${data}`, 22 | { 23 | flag: "a", 24 | }, 25 | (err) => { 26 | if (err) { 27 | console.log(err); 28 | return; 29 | } 30 | console.log("finish writing"); 31 | } 32 | ); 33 | }); 34 | }); 35 | 36 | console.log("end file"); 37 | -------------------------------------------------------------------------------- /core-modules/fs/data/first.txt: -------------------------------------------------------------------------------- 1 | Hello world -------------------------------------------------------------------------------- /core-modules/fs/data/fourth.txt: -------------------------------------------------------------------------------- 1 | this is the fourth file My new filethis is the fourth file My new filethis is the fourth file Javacsript from the serverthis is the fourth file Javacsript from the serverthis is the fourth file Javacsript from the server -------------------------------------------------------------------------------- /core-modules/fs/data/second.txt: -------------------------------------------------------------------------------- 1 | Javacsript from the server -------------------------------------------------------------------------------- /core-modules/fs/data/third.txt: -------------------------------------------------------------------------------- 1 | this is the third file -------------------------------------------------------------------------------- /core-modules/fs/sync.js: -------------------------------------------------------------------------------- 1 | console.log("start file"); 2 | const { readFileSync, writeFileSync } = require("fs"); 3 | 4 | const first = readFileSync("./data/first.txt", "utf8"); 5 | const second = readFileSync("./data/second.txt", "utf8"); 6 | 7 | // console.log(first, second); 8 | // console.log(`${first} from ${second}`); 9 | 10 | const title = "My new file"; 11 | 12 | writeFileSync("./data/fourth.txt", `this is the fourth file ${title}`, { 13 | flag: "a", 14 | }); 15 | console.log("finish reading"); 16 | 17 | console.log("end file"); 18 | -------------------------------------------------------------------------------- /core-modules/http.js: -------------------------------------------------------------------------------- 1 | 2 | const http = require("http"); 3 | 4 | const server = http.createServer((req, res) => { 5 | if (req.url === "/") { 6 | res.write("Welcome to the server"); 7 | return res.end(); 8 | } 9 | 10 | if (req.url === "/about") { 11 | return res.end("About page"); 12 | } 13 | 14 | res.end(` 15 |

Not Found

16 |

The page you are looking for was not found.

17 | Go back to the home page 18 | `); 19 | }); 20 | 21 | server.listen(3000); 22 | console.log("Server is running on port 3000"); -------------------------------------------------------------------------------- /core-modules/os.js: -------------------------------------------------------------------------------- 1 | const os = require("os"); 2 | 3 | console.log(os.userInfo()); 4 | console.log("system up time: ", os.uptime()); 5 | console.log("OS:", os.platform()); 6 | console.log("OS version:", os.release()); 7 | console.log("total Memory:", os.totalmem()); 8 | console.log("free Memory:", os.freemem()); 9 | 10 | console.table({ 11 | os: os.platform(), 12 | version: os.release(), 13 | totalMemory: os.totalmem(), 14 | }); 15 | -------------------------------------------------------------------------------- /core-modules/path.js: -------------------------------------------------------------------------------- 1 | const path = require("path"); 2 | 3 | console.log(path.sep); 4 | 5 | const filePath = path.join("/public", "dist", "/styles/", "main.css"); 6 | console.log(filePath); 7 | 8 | // just the name of the file 9 | console.log(path.basename(filePath)); 10 | 11 | console.log(path.extname(filePath)); 12 | 13 | console.log(path.dirname(filePath)); 14 | 15 | console.log(path.parse(filePath)); 16 | 17 | console.log(path.format(path.parse(filePath))); 18 | 19 | console.log(path.join("../../dist", "styles", "main.css")); 20 | 21 | console.log(path.resolve("../../dist")); 22 | -------------------------------------------------------------------------------- /event-loop/blocking_code.js: -------------------------------------------------------------------------------- 1 | const http = require("http"); 2 | 3 | const server = http.createServer((req, res) => { 4 | if (req.url === "/") { 5 | res.write("Welcome to the server"); 6 | return res.end(); 7 | } 8 | 9 | if (req.url === "/about") { 10 | // blocking code 11 | console.log("start"); 12 | for (let i = 0; i < 100000; i++) { 13 | console.log(Math.random() * i); 14 | } 15 | 16 | return res.end("About page"); 17 | } 18 | 19 | res.end(` 20 |

Not Found

21 |

The page you are looking for was not found.

22 | Go back to the home page 23 | `); 24 | }); 25 | 26 | server.listen(3000); 27 | console.log("Server is running on port 3000"); 28 | -------------------------------------------------------------------------------- /event-loop/setTimeout.js: -------------------------------------------------------------------------------- 1 | console.log("first"); 2 | 3 | setTimeout(() => { 4 | console.log("second"); 5 | }, 0); 6 | 7 | console.log("third"); 8 | -------------------------------------------------------------------------------- /events/http.js: -------------------------------------------------------------------------------- 1 | const http = require("http"); 2 | 3 | const server = http.createServer(); 4 | 5 | server.on("request", (req, res) => { 6 | res.write("Hello World"); 7 | res.end(); 8 | }); 9 | 10 | server.listen(3000); 11 | console.log("Server is running on port 3000"); 12 | -------------------------------------------------------------------------------- /events/index.js: -------------------------------------------------------------------------------- 1 | const EventEmitter = require("events"); 2 | 3 | const customEmitter = new EventEmitter(); 4 | 5 | 6 | customEmitter.emit("response", "Hello World"); 7 | customEmitter.emit("response", "Hello World 2"); 8 | customEmitter.emit("response", "Hello World 3", 30); 9 | customEmitter.emit("response", "Hello World 4", [1, 2, 3]); 10 | customEmitter.on("response", (data, secondParameter) => { 11 | console.log(data, secondParameter); 12 | }); -------------------------------------------------------------------------------- /file-system/read.js: -------------------------------------------------------------------------------- 1 | const fs = require('fs'); 2 | 3 | fs.readFile('./fileOne.txt', (err, data) => { 4 | if (err) { 5 | console.log(err); 6 | } else { 7 | console.log(data.toString()); 8 | } 9 | }); 10 | 11 | console.log('The last line of the program'); 12 | -------------------------------------------------------------------------------- /file-system/write.js: -------------------------------------------------------------------------------- 1 | const fs = require('fs'); 2 | 3 | fs.writeFile('./fileOne.txt', 'line one\nline two', error => { 4 | if(error) { 5 | console.log(error); 6 | } 7 | else { 8 | console.log('the file was created'); 9 | } 10 | }); 11 | 12 | console.log('last line of the program'); 13 | -------------------------------------------------------------------------------- /globals/globals.js: -------------------------------------------------------------------------------- 1 | console.log(__dirname) 2 | console.log(__filename) 3 | 4 | console.log(module) 5 | console.log(require) 6 | 7 | console.log(process) -------------------------------------------------------------------------------- /globals/timers.js: -------------------------------------------------------------------------------- 1 | // setInterval(() => { 2 | // console.log("Hello"); 3 | // }, 1000); 4 | 5 | setTimeout(() => { 6 | console.log("Finished!"); 7 | }, 3000); -------------------------------------------------------------------------------- /hello-world/index.js: -------------------------------------------------------------------------------- 1 | console.log('hello world'); 2 | -------------------------------------------------------------------------------- /http/index.js: -------------------------------------------------------------------------------- 1 | const http = require('http'); 2 | 3 | const server = http.createServer((req, res) => { 4 | res.writeHead(200, {'Content-Type': 'text/html'}); 5 | res.write(`

Hello world from a Server

`); 6 | res.end(); 7 | }); 8 | 9 | server.listen(3000, () => { 10 | console.log('Server is working!'); 11 | }); 12 | -------------------------------------------------------------------------------- /interfacing-with-io/greeting.js: -------------------------------------------------------------------------------- 1 | process.stdin.on("data", (data) => { 2 | const name = data.toString().trim().toUpperCase(); 3 | 4 | if (name !== "") { 5 | process.stdout.write(`Hello ${name}`); 6 | } else { 7 | process.stderr.write("Input is empty"); 8 | } 9 | }); 10 | -------------------------------------------------------------------------------- /javascript_basics/conditionals.js: -------------------------------------------------------------------------------- 1 | const age = 20; 2 | 3 | if (age >= 18) { 4 | console.log("You are an adult"); 5 | } else if (age >= 13) { 6 | console.log("You are a teenager"); 7 | } else { 8 | console.log("You are a child"); 9 | } 10 | -------------------------------------------------------------------------------- /javascript_basics/functions.js: -------------------------------------------------------------------------------- 1 | const showUserInfo = (userName, userAge) => 2 | `The username is ${userName}, the user is ${userAge} years old`; 3 | 4 | console.log(showUserInfo("Max", 27)); 5 | console.log(showUserInfo("Manu", 29)); 6 | console.log(showUserInfo("Anna", 30)); 7 | console.log(showUserInfo("Joe", 20)); 8 | -------------------------------------------------------------------------------- /javascript_basics/loops.js: -------------------------------------------------------------------------------- 1 | const names = ["John", "Paul", "George", "Ringo"]; 2 | 3 | for (let i = 0; i < names.length; i++) { 4 | console.log(names[i]); 5 | } 6 | -------------------------------------------------------------------------------- /javascript_basics/variables.js: -------------------------------------------------------------------------------- 1 | let username = "Ryan"; // string 2 | let age = 20; // number 3 | let hasHobbies = true; // boolean 4 | let points = [10, 20, 30]; // arrays 5 | let user = { 6 | //objects 7 | name: "name", 8 | lasname: "ray", 9 | }; 10 | const PI = 3.1415; 11 | 12 | console.log(username); 13 | console.log(age); 14 | console.log(hasHobbies); 15 | console.log(points); 16 | console.log(user); 17 | console.log(PI); 18 | -------------------------------------------------------------------------------- /modules/example1/main.js: -------------------------------------------------------------------------------- 1 | const { myArray, myNumber, myWebAddress, user } = require("./myModule"); 2 | 3 | console.log(require('./myModule')) 4 | 5 | console.log(myArray); 6 | console.log(myNumber); 7 | console.log(myWebAddress); 8 | console.log(user); 9 | -------------------------------------------------------------------------------- /modules/example1/myModule.js: -------------------------------------------------------------------------------- 1 | console.log(module) 2 | const mySecret = 'secret123xyz'; 3 | 4 | const myWebAddress = "faztweb.com"; 5 | const myNumber = 300; 6 | const myArray = [100, 20, true]; 7 | const user = { 8 | name: "Fazt", 9 | age: 100, 10 | }; 11 | 12 | module.exports = { 13 | myWebAddress, 14 | myNumber, 15 | myArray, 16 | user, 17 | }; 18 | 19 | console.log(module) -------------------------------------------------------------------------------- /modules/index.js: -------------------------------------------------------------------------------- 1 | const math = require("./math"); 2 | 3 | // add 4 | console.log(math.add(1, 2)); 5 | console.log(math.substract(1, 2)); 6 | console.log(math.division(1, 2)); 7 | console.log(math.division(1, 0)); 8 | console.log(math.multiply(1, 2)); 9 | -------------------------------------------------------------------------------- /modules/math.js: -------------------------------------------------------------------------------- 1 | const PI = 3.14; 2 | 3 | function add(x, y) { 4 | return x + y; 5 | } 6 | 7 | function substract(x, y) { 8 | return x - y; 9 | } 10 | 11 | function division(x, y) { 12 | if (y == 0) { 13 | showMessage("You cannot divide by 0"); 14 | } else { 15 | return x / y; 16 | } 17 | } 18 | 19 | function multiply(x, y) { 20 | return x * y; 21 | } 22 | 23 | function showMessage(message) { 24 | console.log(message); 25 | } 26 | 27 | console.log(module) 28 | 29 | exports.add = add; 30 | exports.substract = substract; 31 | exports.division = division; 32 | exports.multiply = multiply; 33 | 34 | 35 | console.log(module) -------------------------------------------------------------------------------- /promises/async_await.js: -------------------------------------------------------------------------------- 1 | 2 | const { readFile } = require("fs"); 3 | 4 | const getText = (pathFile) => { 5 | return new Promise((resolve, rejext) => { 6 | readFile(pathFile, "utf8", (err, data) => { 7 | if (err) { 8 | rejext(err); 9 | } else { 10 | resolve(data); 11 | } 12 | }); 13 | }); 14 | }; 15 | 16 | async function read() { 17 | try { 18 | const result = await getText("./data/first.txt"); 19 | console.log(result); 20 | } catch (error) { 21 | console.error(error); 22 | } 23 | } 24 | 25 | read(); 26 | -------------------------------------------------------------------------------- /promises/data/first.txt: -------------------------------------------------------------------------------- 1 | Hello from file -------------------------------------------------------------------------------- /promises/data/second.txt: -------------------------------------------------------------------------------- 1 | Hello from file -------------------------------------------------------------------------------- /promises/data/third.txt: -------------------------------------------------------------------------------- 1 | Hello from file -------------------------------------------------------------------------------- /promises/promises.js: -------------------------------------------------------------------------------- 1 | const { readFile } = require("fs"); 2 | 3 | const getText = (pathFile) => { 4 | return new Promise((resolve, rejext) => { 5 | readFile(pathFile, "utf8", (err, data) => { 6 | if (err) { 7 | rejext(err); 8 | } else { 9 | resolve(data); 10 | } 11 | }); 12 | }); 13 | }; 14 | 15 | getText("./data/first.txt") 16 | .then((result) => console.log(result)) 17 | .catch((err) => console.log(err)); 18 | -------------------------------------------------------------------------------- /promises/promisify.js: -------------------------------------------------------------------------------- 1 | const { readFile, writeFile } = require("fs/promises"); 2 | 3 | async function read() { 4 | try { 5 | const result = await readFile("./data/first.txt", "utf8"); 6 | await writeFile("./data/third.txt", result); 7 | console.log(result); 8 | } catch (error) { 9 | console.error(error); 10 | } 11 | } 12 | 13 | read(); 14 | -------------------------------------------------------------------------------- /repl/names.js: -------------------------------------------------------------------------------- 1 | const names = ['joe', 'john', 'maria'] 2 | names.map(name => `Hola ${name}`) 3 | const hellonames= names.map(name => `Hola ${name}`) 4 | hellonames -------------------------------------------------------------------------------- /static-file-server/index.js: -------------------------------------------------------------------------------- 1 | const http = require('http'), 2 | url = require('url'), 3 | fs = require('fs'); 4 | 5 | const server = http.createServer((req, res) => { 6 | const objURL = url.parse(req.url); 7 | let folderPath = __dirname + '/static' + objURL.pathname; 8 | if(objURL.pathname === '/') { 9 | folderPath = __dirname + '/static/index.html'; 10 | } 11 | fs.stat(folderPath, (err, stats) => { 12 | if(!err) { 13 | fs.readFile(folderPath, (err, content) => { 14 | if(err) { 15 | res.writeHead(500, {'Content-Type': 'text/plain'}); 16 | res.write('Error Interno'); 17 | res.end(); 18 | } else { 19 | res.writeHead(200, {'Content-Type': 'text/html'}); 20 | res.write(content); 21 | res.end(); 22 | } 23 | }); 24 | } else { 25 | res.writeHead(404, {'Content-Type': 'text/html'}); 26 | res.write('

Page Not Found

'); 27 | res.end(); 28 | } 29 | }); 30 | }); 31 | 32 | server.listen(3000, () => console.log('server is listening on port 3000')); 33 | -------------------------------------------------------------------------------- /static-file-server/static/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | My First Page 6 | 7 | 8 | Page 1 9 | Page 2 10 | 11 | 12 | -------------------------------------------------------------------------------- /static-file-server/static/page1.html: -------------------------------------------------------------------------------- 1 |

Page One

2 | -------------------------------------------------------------------------------- /static-file-server/static/page2.html: -------------------------------------------------------------------------------- 1 |

Page2

2 | -------------------------------------------------------------------------------- /streams/createBigFile.js: -------------------------------------------------------------------------------- 1 | const { writeFile } = require("fs/promises"); 2 | 3 | const createBigFile = async () => { 4 | await writeFile("bigFile.txt", "Hello World".repeat(1000000)); 5 | }; 6 | 7 | createBigFile(); 8 | -------------------------------------------------------------------------------- /streams/http.js: -------------------------------------------------------------------------------- 1 | const http = require("http"); 2 | const fs = require("fs"); 3 | 4 | const server = http.createServer(async (req, res) => { 5 | const fileStream = fs.createReadStream("./bigFile.txt", { encoding: "utf8" }); 6 | fileStream.on("open", (chunk) => { 7 | fileStream.pipe(res); 8 | }); 9 | 10 | fileStream.on("error", (err) => { 11 | console.log(err); 12 | }); 13 | }); 14 | 15 | server.listen(3000); 16 | console.log("Server is running on port 3000"); 17 | -------------------------------------------------------------------------------- /streams/readStream.js: -------------------------------------------------------------------------------- 1 | const { createReadStream } = require("fs"); 2 | 3 | const stream = createReadStream("./bigFile.txt", { 4 | highWaterMark: 90000, 5 | encoding: "utf8" 6 | }); 7 | 8 | stream.on("data", (chunk) => { 9 | // console.log(chunk.length); 10 | console.log(chunk.toString()); 11 | // console.log(chunk) 12 | }); 13 | 14 | stream.on("end", () => { 15 | console.log("end"); 16 | }); 17 | 18 | stream.on("error", (err) => { 19 | console.log(err); 20 | }); 21 | -------------------------------------------------------------------------------- /url/index.js: -------------------------------------------------------------------------------- 1 | const http = require('http'); 2 | const url = require('url'); 3 | 4 | const server = http.createServer((req, res) => { 5 | console.log(url); 6 | const objectUrl = url.parse(req.url); 7 | console.log(objectUrl); 8 | console.log('Pathname:', objectUrl.pathname); 9 | console.log('Path:', objectUrl.path); 10 | console.log('Query:', objectUrl.query); 11 | res.writeHead(200, {'Content-Type': 'text/html'}); 12 | res.write('

Request Received

'); 13 | res.end(); 14 | }); 15 | 16 | server.listen(3000, () => { 17 | console.log('server is listening on port 3000'); 18 | }); 19 | --------------------------------------------------------------------------------