Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
lifeisfoo committed Mar 8, 2024
0 parents commit e1558b3
Show file tree
Hide file tree
Showing 225 changed files with 15,593 additions and 0 deletions.
7 changes: 7 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@

root = true

[*]
indent_style = space
indent_size = 4

1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules
4 changes: 4 additions & 0 deletions 02/hello.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
function hello(name) {
console.log(`Hello ${name}`);
}
hello("Node.js");
4 changes: 4 additions & 0 deletions 03-fortune/01-fortune-single.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
const quote =
"Any app that can be written in JavaScript, will eventually be written in JavaScript. Jeff Atwood";

console.log(quote);
7 changes: 7 additions & 0 deletions 03-fortune/02-fortune-single-multiline.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const quote = `
Any app that can be written in JavaScript,
will eventually be written in JavaScript.
-- Jeff Atwood.
`;

console.log(quote);
18 changes: 18 additions & 0 deletions 03-fortune/03-fortune-multi.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
const quotes = [
`Any app that can be written in JavaScript,
will eventually be written in JavaScript.
-- Jeff Atwood.
`,
`JavaScript is the only language that I'm aware of
that people feel they don't need to learn before
they start using it.
-- Douglas Crockford
`,
`Code never lies, comments sometimes do.
-- Anonymous
`,
];

const randomIdx = Math.floor(Math.random() * quotes.length);

console.log(quotes[randomIdx]);
5 changes: 5 additions & 0 deletions 03-fortune/04-fortune-multi-json.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const quotes = require("./quotes.json");

const randomIdx = Math.floor(Math.random() * quotes.length);

console.log(quotes[randomIdx]);
9 changes: 9 additions & 0 deletions 03-fortune/05-read-file.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const fs = require("fs");

fs.readFile("./data/003.txt", "utf-8", (err, data) => {
if (err) {
console.log("Error while reading quote file");
return;
}
console.log(data);
});
9 changes: 9 additions & 0 deletions 03-fortune/06-read-dir.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const fs = require("fs");

fs.readdir("./data", (err, files) => {
if (err) {
console.log("Error while reading data directory");
return;
}
console.log(files);
});
21 changes: 21 additions & 0 deletions 03-fortune/07-fortune-cb.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
const fs = require("fs");

const QUOTES_DIR = "./data";

fs.readdir(QUOTES_DIR, (err, files) => {
if (err) {
console.log("Error while reading data directory");
return;
}

const randomIdx = Math.floor(Math.random() * files.length);
const quoteFile = `${QUOTES_DIR}/${files[randomIdx]}`;

fs.readFile(quoteFile, "utf-8", (err, data) => {
if (err) {
console.log("Error while reading quote file");
return;
}
console.log(data.toString());
});
});
23 changes: 23 additions & 0 deletions 03-fortune/08-fortune-exit-code.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
const fs = require("fs");

const QUOTES_DIR = "./data";

fs.readdir(QUOTES_DIR, (err, files) => {
if (err) {
console.log("Error while reading data directory");
process.exitCode = 1;
return;
}

const randomIdx = Math.floor(Math.random() * files.length);
const quoteFile = `${QUOTES_DIR}/${files[randomIdx]}`;

fs.readFile(quoteFile, "utf-8", (err, data) => {
if (err) {
console.log("Error while reading quote file");
process.exitCode = 1;
return;
}
console.log(data);
});
});
23 changes: 23 additions & 0 deletions 03-fortune/09-fortune-console-error.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
const fs = require("fs");

const QUOTES_DIR = "./data";

fs.readdir(QUOTES_DIR, (err, files) => {
if (err) {
console.error(`Error while reading ${QUOTES_DIR} directory`);
process.exitCode = 1;
return;
}

const randomIdx = Math.floor(Math.random() * files.length);
const quoteFile = `${QUOTES_DIR}/${files[randomIdx]}`;

fs.readFile(quoteFile, "utf-8", (err, data) => {
if (err) {
console.error(`Error while reading ${quoteFile} file`);
process.exitCode = 1;
return;
}
console.log(data.toString());
});
});
27 changes: 27 additions & 0 deletions 03-fortune/10-fortune-txt-files-only.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
const fs = require("fs");

const QUOTES_DIR = "./data";

fs.readdir(QUOTES_DIR, { withFileTypes: true }, (err, files) => {
if (err) {
console.error(`Error while reading ${QUOTES_DIR} directory`);
process.exitCode = 1;
return;
}

const txtFiles = files
.filter((f) => f.isFile() && f.name.endsWith(".txt"))
.map((f) => f.name);

const randomIdx = Math.floor(Math.random() * txtFiles.length);
const quoteFile = `${QUOTES_DIR}/${txtFiles[randomIdx]}`;

fs.readFile(quoteFile, "utf-8", (err, data) => {
if (err) {
console.error(`Error while reading ${quoteFile} file`);
process.exitCode = 1;
return;
}
console.log(data.toString());
});
});
27 changes: 27 additions & 0 deletions 03-fortune/11-fortune-argv.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
const fs = require("fs");

const QUOTES_DIR = process.argv[2];

fs.readdir(QUOTES_DIR, { withFileTypes: true }, (err, files) => {
if (err) {
console.error(`Error while reading ${QUOTES_DIR} directory`);
process.exitCode = 1;
return;
}

const txtFiles = files
.filter((f) => f.isFile() && f.name.endsWith(".txt"))
.map((f) => f.name);

const randomIdx = Math.floor(Math.random() * txtFiles.length);
const quoteFile = `${QUOTES_DIR}/${txtFiles[randomIdx]}`;

fs.readFile(quoteFile, "utf-8", (err, data) => {
if (err) {
console.error(`Error while reading ${quoteFile} file`);
process.exitCode = 1;
return;
}
console.log(data.toString());
});
});
23 changes: 23 additions & 0 deletions 03-fortune/12-fortune-promises.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
const fs = require("fs/promises");

const QUOTES_DIR = process.argv[2];

fs.readdir(QUOTES_DIR, { withFileTypes: true })
.then((files) => {
const txtFiles = files
.filter((f) => f.isFile() && f.name.endsWith(".txt"))
.map((f) => f.name);

const randomIdx = Math.floor(Math.random() * txtFiles.length);
const quoteFile = `${QUOTES_DIR}/${txtFiles[randomIdx]}`;

return fs.readFile(quoteFile, "utf-8");
})
.then((data) => {
console.log(data.toString());
})
.catch((err) => {
console.error(`Error: ${err.message}`);
process.exitCode = 1;
return;
});
19 changes: 19 additions & 0 deletions 03-fortune/13-fortune-async-await.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import fs from "fs/promises";

const QUOTES_DIR = process.argv[2];

try {
const files = await fs.readdir(QUOTES_DIR, { withFileTypes: true });
const txtFiles = files
.filter((f) => f.isFile() && f.name.endsWith(".txt"))
.map((f) => f.name);

const randomIdx = Math.floor(Math.random() * txtFiles.length);
const quoteFile = `${QUOTES_DIR}/${txtFiles[randomIdx]}`;

const data = await fs.readFile(quoteFile, "utf-8");
console.log(data.toString());
} catch (err) {
console.error(`Error: ${err.message}`);
process.exitCode = 1;
}
3 changes: 3 additions & 0 deletions 03-fortune/data/001.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Non si abita un Paese, si abita una lingua.
Una patria è questo, e niente altro.
-- Emil Cioran
3 changes: 3 additions & 0 deletions 03-fortune/data/002.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Pubblicare un libro comporta lo stesso genere di noie
di un matrimonio o di un funerale.
-- Emil Cioran
3 changes: 3 additions & 0 deletions 03-fortune/data/003.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Un libro che lascia il lettore uguale
a com'era prima di leggerlo è un libro fallito.
-- Emil Cioran
2 changes: 2 additions & 0 deletions 03-fortune/data/004.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Si tira un aforisma come si tira uno schiaffo.
-- Emil Cioran
3 changes: 3 additions & 0 deletions 03-fortune/data/005.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Non si scrive perché si ha qualcosa da dire
ma perché si ha voglia di dire qualcosa.
-- Emil Cioran
3 changes: 3 additions & 0 deletions 03-fortune/data/006.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Il fatto che la vita non abbia alcun senso è una ragione di vivere
– la sola, del resto.
-- Emil Cioran
5 changes: 5 additions & 0 deletions 03-fortune/quotes.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[
"Any app that can be written in JavaScript,\nwill eventually be written in JavaScript.\n\t\t\t-- Jeff Atwood",
"JavaScript is the only language that I'm aware of\nthat people feel they don't need to learn before\nthey start using it.\n\t\t\t-- Douglas Crockford",
"Code never lies, comments sometimes do.\n\t\t\t-- Anonymous"
]
9 changes: 9 additions & 0 deletions 04-library/01-min.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const http = require("http");

const server = http.createServer((req, res) => {
res.end("Benvenuto nella biblioteca HTTP");
});

server.listen(3000, () => {
console.log("Server running");
});
13 changes: 13 additions & 0 deletions 04-library/02-min-clear.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
const http = require("http");

const host = "127.0.0.1";
const port = 3000;

const server = http.createServer((req, res) => {
res.statusCode = 200;
res.end("Benvenuto nella biblioteca HTTP");
});

server.listen(port, host, () => {
console.log(`Server running at http://${host}:${port}/`);
});
13 changes: 13 additions & 0 deletions 04-library/03-json.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
const http = require("http");

const host = "127.0.0.1";
const port = 3000;

const server = http.createServer((req, res) => {
res.statusCode = 200;
res.end('{"message": "Benvenuto nella biblioteca HTTP"}');
});

server.listen(port, host, () => {
console.log(`Server running at http://${host}:${port}/`);
});
14 changes: 14 additions & 0 deletions 04-library/04-json-clear.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
const http = require("http");

const host = "127.0.0.1";
const port = 3000;

const server = http.createServer((req, res) => {
res.statusCode = 200;
res.setHeader("Content-Type", "application/json");
res.end(JSON.stringify({ message: "Benvenuto nella biblioteca HTTP" }));
});

server.listen(port, host, () => {
console.log(`Server running at http://${host}:${port}/`);
});
26 changes: 26 additions & 0 deletions 04-library/05-accept.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
const http = require("http");

const host = "127.0.0.1";
const port = 3000;

const server = http.createServer((req, res) => {
const acceptJson = req.headers.accept === "application/json";
const acceptText = req.headers.accept === "text/plain";

if (acceptJson) {
res.statusCode = 200;
res.setHeader("Content-Type", "application/json");
res.end(JSON.stringify({ message: "Benvenuto nella biblioteca HTTP" }));
} else if (acceptText) {
res.statusCode = 200;
res.setHeader("Content-Type", "text/plain");
res.end("Benvenuto nella biblioteca HTTP");
} else {
res.statusCode = 406;
res.end();
}
});

server.listen(port, host, () => {
console.log(`Server running at http://${host}:${port}/`);
});
36 changes: 36 additions & 0 deletions 04-library/06-accept-types.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
const http = require("http");

const host = "127.0.0.1";
const port = 3000;

function resJson(res) {
res.statusCode = 200;
res.setHeader("Content-Type", "application/json");
res.end(JSON.stringify({ message: "Benvenuto nella biblioteca HTTP" }));
}

function resText(res) {
res.statusCode = 200;
res.setHeader("Content-Type", "text/plain");
res.end("Benvenuto nella biblioteca HTTP");
}

const server = http.createServer((req, res) => {
const acceptJson = req.headers.accept === "application/json";
const acceptText = req.headers.accept === "text/plain";
const acceptAnyText = req.headers.accept === "text/*";
const acceptAnyType = req.headers.accept === "*/*";

if (acceptJson) {
resJson(res);
} else if (acceptText || acceptAnyText || acceptAnyType) {
resText(res);
} else {
res.statusCode = 406;
res.end();
}
});

server.listen(port, host, () => {
console.log(`Server running at http://${host}:${port}/`);
});
Loading

0 comments on commit e1558b3

Please sign in to comment.