mirror of
https://github.com/kevin-DL/complete-node-bootcamp.git
synced 2026-01-11 19:14:26 +00:00
Initial commit 🚀
This commit is contained in:
33
2-how-node-works/final/event-loop.js
Normal file
33
2-how-node-works/final/event-loop.js
Normal file
@@ -0,0 +1,33 @@
|
||||
const fs = require("fs");
|
||||
const crypto = require("crypto");
|
||||
|
||||
const start = Date.now();
|
||||
process.env.UV_THREADPOOL_SIZE = 4;
|
||||
|
||||
setTimeout(() => console.log("Timer 1 finished"), 0);
|
||||
setImmediate(() => console.log("Immediate 1 finished"));
|
||||
|
||||
fs.readFile("test-file.txt", () => {
|
||||
console.log("I/O finished");
|
||||
console.log("----------------");
|
||||
|
||||
setTimeout(() => console.log("Timer 2 finished"), 0);
|
||||
setTimeout(() => console.log("Timer 3 finished"), 3000);
|
||||
setImmediate(() => console.log("Immediate 2 finished"));
|
||||
|
||||
process.nextTick(() => console.log("Process.nextTick"));
|
||||
|
||||
crypto.pbkdf2Sync("password", "salt", 100000, 1024, "sha512");
|
||||
console.log(Date.now() - start, "Password encrypted");
|
||||
|
||||
crypto.pbkdf2Sync("password", "salt", 100000, 1024, "sha512");
|
||||
console.log(Date.now() - start, "Password encrypted");
|
||||
|
||||
crypto.pbkdf2Sync("password", "salt", 100000, 1024, "sha512");
|
||||
console.log(Date.now() - start, "Password encrypted");
|
||||
|
||||
crypto.pbkdf2Sync("password", "salt", 100000, 1024, "sha512");
|
||||
console.log(Date.now() - start, "Password encrypted");
|
||||
});
|
||||
|
||||
console.log("Hello from the top-level code");
|
||||
46
2-how-node-works/final/events.js
Normal file
46
2-how-node-works/final/events.js
Normal file
@@ -0,0 +1,46 @@
|
||||
const EventEmitter = require("events");
|
||||
const http = require("http");
|
||||
|
||||
class Sales extends EventEmitter {
|
||||
constructor() {
|
||||
super();
|
||||
}
|
||||
}
|
||||
|
||||
const myEmitter = new Sales();
|
||||
|
||||
myEmitter.on("newSale", () => {
|
||||
console.log("There was a new sale!");
|
||||
});
|
||||
|
||||
myEmitter.on("newSale", () => {
|
||||
console.log("Costumer name: Jonas");
|
||||
});
|
||||
|
||||
myEmitter.on("newSale", stock => {
|
||||
console.log(`There are now ${stock} items left in stock.`);
|
||||
});
|
||||
|
||||
myEmitter.emit("newSale", 9);
|
||||
|
||||
//////////////////
|
||||
|
||||
const server = http.createServer();
|
||||
|
||||
server.on("request", (req, res) => {
|
||||
console.log("Request received!");
|
||||
console.log(req.url);
|
||||
res.end("Request received");
|
||||
});
|
||||
|
||||
server.on("request", (req, res) => {
|
||||
console.log("Another request 😀");
|
||||
});
|
||||
|
||||
server.on("close", () => {
|
||||
console.log("Server closed");
|
||||
});
|
||||
|
||||
server.listen(8000, "127.0.0.1", () => {
|
||||
console.log("Waiting for requests...");
|
||||
});
|
||||
17
2-how-node-works/final/modules.js
Normal file
17
2-how-node-works/final/modules.js
Normal file
@@ -0,0 +1,17 @@
|
||||
// console.log(arguments);
|
||||
// console.log(require("module").wrapper);
|
||||
|
||||
// module.exports
|
||||
const C = require("./test-module-1");
|
||||
const calc1 = new C();
|
||||
console.log(calc1.add(2, 5));
|
||||
|
||||
// exports
|
||||
// const calc2 = require("./test-module-2");
|
||||
const { add, multiply } = require("./test-module-2");
|
||||
console.log(multiply(2, 5));
|
||||
|
||||
// caching
|
||||
require("./test-module-3")();
|
||||
require("./test-module-3")();
|
||||
require("./test-module-3")();
|
||||
33
2-how-node-works/final/streams.js
Normal file
33
2-how-node-works/final/streams.js
Normal file
@@ -0,0 +1,33 @@
|
||||
const fs = require("fs");
|
||||
const server = require("http").createServer();
|
||||
|
||||
server.on("request", (req, res) => {
|
||||
// Solution 1
|
||||
// fs.readFile("test-file.txt", (err, data) => {
|
||||
// if (err) console.log(err);
|
||||
// res.end(data);
|
||||
// });
|
||||
|
||||
// Solution 2: Streams
|
||||
// const readable = fs.createReadStream("test-file.txt");
|
||||
// readable.on("data", chunk => {
|
||||
// res.write(chunk);
|
||||
// });
|
||||
// readable.on("end", () => {
|
||||
// res.end();
|
||||
// });
|
||||
// readable.on("error", err => {
|
||||
// console.log(err);
|
||||
// res.statusCode = 500;
|
||||
// res.end("File not found!");
|
||||
// });
|
||||
|
||||
// Solution 3
|
||||
const readable = fs.createReadStream("test-file.txt");
|
||||
readable.pipe(res);
|
||||
// readableSource.pipe(writeableDest)
|
||||
});
|
||||
|
||||
server.listen(8000, "127.0.0.1", () => {
|
||||
console.log("Listening...");
|
||||
});
|
||||
1000001
2-how-node-works/final/test-file.txt
Normal file
1000001
2-how-node-works/final/test-file.txt
Normal file
File diff suppressed because it is too large
Load Diff
27
2-how-node-works/final/test-module-1.js
Normal file
27
2-how-node-works/final/test-module-1.js
Normal file
@@ -0,0 +1,27 @@
|
||||
// class Calculator {
|
||||
// add(a, b) {
|
||||
// return a + b;
|
||||
// }
|
||||
|
||||
// multiply(a, b) {
|
||||
// return a * b;
|
||||
// }
|
||||
|
||||
// divide(a, b) {
|
||||
// return a / b;
|
||||
// }
|
||||
// }
|
||||
|
||||
module.exports = class {
|
||||
add(a, b) {
|
||||
return a + b;
|
||||
}
|
||||
|
||||
multiply(a, b) {
|
||||
return a * b;
|
||||
}
|
||||
|
||||
divide(a, b) {
|
||||
return a / b;
|
||||
}
|
||||
};
|
||||
3
2-how-node-works/final/test-module-2.js
Normal file
3
2-how-node-works/final/test-module-2.js
Normal file
@@ -0,0 +1,3 @@
|
||||
exports.add = (a, b) => a + b;
|
||||
exports.multiply = (a, b) => a * b;
|
||||
exports.divide = (a, b) => a / b;
|
||||
3
2-how-node-works/final/test-module-3.js
Normal file
3
2-how-node-works/final/test-module-3.js
Normal file
@@ -0,0 +1,3 @@
|
||||
console.log("Hello from the module");
|
||||
|
||||
module.exports = () => console.log("Log this beautiful text 😍");
|
||||
1000001
2-how-node-works/starter/test-file.txt
Normal file
1000001
2-how-node-works/starter/test-file.txt
Normal file
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user