8000 NodeJS Basic tutorial · dev-grid/NodeJS-Tutorial@248ec00 · GitHub
[go: up one dir, main page]

Skip to content

Commit 248ec00

Browse files
committed
NodeJS Basic tutorial
0 parents  commit 248ec00

28 files changed

+4960
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
node_modules

0-console/0-console.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
const displayMessage = (msg) => {
2+
console.log(msg);
3+
}
4+
5+
module.exports = displayMessage;

0-console/0-main.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
const displayMessage = require('./0-console');
2+
3+
displayMessage("Hello NodeJS!");

1-stdin/1-stdin.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
process.stdout.write("Welcome to Holberton School, what is your name?\n");
2+
3+
if (process.stdin.isTTY){
4+
process.stdin.on('data', (data) =>{
5+
process.stdout.write(`Your name is: ${data.toString()}`);
6+
process.exit();
7+
});
8+
} else {
9+
process.stdin.on('data', (data) =>{
10+
process.stdout.write(`Your name is: ${data.toString()}`);
11+
process.exit();
12+
});
13+
process.on('exit', () =>{
14+
process.stdout.write('This important software is now closing\n');
15+
});
16+
};

2-read_file/2-main_0.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
const countStudents = require('./2-read_file');
2+
3+
countStudents("nope.csv");

2-read_file/2-main_1.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
const countStudents = require('./2-read_file');
2+
3+
countStudents("database.csv");

2-read_file/2-read_file.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
const fs = require('fs');
2+
3+
function countStudents(fileName){
4+
const students = {};
5+
const fields = {};
6+
let length = 0; //var, let, const
7+
try{
8+
const content = fs.readFileSync(fileName, 'utf-8');
9+
const lines = content.toString().split('\n');
10+
for (let i = 0; i < lines.length; i += 1){
11+
if (lines[i]){
12+
length += 1;
13+
const field = lines[i].toString().split(',');
14+
if (Object.prototype.hasOwnProperty.call(students, field[3])){
15+
students[field[3]].push(field[0]);
16+
}else {
17+
students[field[3]] = [field[0]];
18+
}
19+
if(Object.prototype.hasOwnProperty.call(fields, field[3])){
20+
fields[field[3]] += 1;
21+
} else {
22+
fields[field[3]] = 1;
23+
}
24+
}
25+
}
26+
const l = length - 1;
27+
console.log(`Number of students: ${l}`);
28+
for (const [key, value] of Object.entries(fields) ){
29+
if (key !== 'field'){
30+
console.log(`Number of students in ${key}: ${value}. List: ${students[key].join(', ')}`);
31+
}
32+
}
33+
34+
} catch (error){
35+
throw new Error('Cannot load the database');
36+
}
37+
}
38+
39+
module.exports = countStudents;

2-read_file/database.csv

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
firstname,lastname,age,field
2+
Johann,Kerbrou,30,CS
3+
Guillaume,Salou,30,SWE
4+
Arielle,Salou,20,CS
5+
Jonathan,Benou,30,CS
6+
Emmanuel,Turlou,40,CS
7+
Guillaume,Plessous,35,CS
8+
Joseph,Crisou,34,SWE
9+
Paul,Schneider,60,SWE
10+
Tommy,Schoul,32,SWE
11+
Katie,Shirou,21,CS

3-read_file_async/3-main_0.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
const countStudents = require('./3-read_file_async');
2+
countStudents("nope.csv")
3+
.then(() => {
4+
console.log("Done!");
5+
})
6+
.catch((error) => {
7+
console.log(error);
8+
});

3-read_file_async/3-main_1.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
const countStudents = require('./3-read_file_async');
2+
countStudents("database.csv")
3+
.then(() => {
4+
console.log("Done!");
5+
})
6+
.catch((error) => {
7+
console.log(error);
8+
});
9+
console.log("After!");
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
const {readFile} = require('fs');
2+
3+
function countStudents(fileName){
4+
const students = {};
5+
const fields = {};
6+
let length = 0; //var, let, const
7+
8+
return new Promise((resolve, reject) => {
9+
readFile(fileName, (error, data) => {
10+
if (error){
11+
reject(Error('Cannot load the database'));
12+
} else{
13+
const lines = data.toString().split('\n');
14+
for (let i = 0; i < lines.length; i += 1){
15+
if (lines[i]){
16+
length += 1;
17+
const field = lines[i].toString().split(',');
18+
if (Object.prototype.hasOwnProperty.call(students, field[3])){
19+
students[field[3]].push(field[0]);
20+
}else {
21+
students[field[3]] = [field[0]];
22+
}
23+
if(Object.prototype.hasOwnProperty.call(fields, field[3])){
24+
fields[field[3]] += 1;
25+
} else {
26+
fields[field[3]] = 1;
27+
}
28+
}
29+
}
30+
const l = length - 1;
31+
console.log(`Number of students: ${l}`);
32+
for (const [key, value] of Object.entries(fields) ){
33+
if (key !== 'field'){
34+
console.log(`Number of students in ${key}: ${value}. List: ${students[key].join(', ')}`);
35+
}
36+
}
37+
resolve(data);
38+
}
39+
})
40+
});
41+
// try{
42+
// const content = fs.readFileSync(fileName, 'utf-8');
43+
// const lines = content.toString().split('\n');
44+
// for (let i = 0; i < lines.length; i += 1){
45+
// if (lines[i]){
46+
// length += 1;
47+
// const field = lines[i].toString().split(',');
48+
// if (Object.prototype.hasOwnProperty.call(students, field[3])){
49+
// students[field[3]].push(field[0]);
50+
// }else {
51+
// students[field[3]] = [field[0]];
52+
// }
53+
// if(Object.prototype.hasOwnProperty.call(fields, field[3])){
54+
// fields[field[3]] += 1;
55+
// } else {
56+
// fields[field[3]] = 1;
57+
// }
58+
// }
59+
// }
60+
// const l = length - 1;
61+
// console.log(`Number of students: ${l}`);
62+
// for (const [key, value] of Object.entries(fields) ){
63+
// if (key !== 'field'){
64+
// console.log(`Number of students in ${key}: ${value}. List: ${students[key].join(', ')}`);
65+
// }
66+
// }
67+
68+
// } catch (error){
69+
// throw new Error('Cannot load the database');
70+
// }
71+
}
72+
73+
module.exports = countStudents;

3-read_file_async/database.csv

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
firstname,lastname,age,field
2+
Johann,Kerbrou,30,CS
3+
Guillaume,Salou,30,SWE
4+
Arielle,Salou,20,CS
5+
Jonathan,Benou,30,CS
6+
Emmanuel,Turlou,40,CS
7+
Guillaume,Plessous,35,CS
8+
Joseph,Crisou,34,SWE
9+
Paul,Schneider,60,SWE
10+
Tommy,Schoul,32,SWE
11+
Katie,Shirou,21,CS

4-http/4-http.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
const http = require ('http');
2+
3+
const port = 1245;
4+
const host = '127.0.0.1';
5+
6+
7+
const app = http.createServer((req, res) => {
8+
res.statusCode = 200;
9+
res.setHeader('Content-Type', 'text/plain');
10+
res.end('Hello Holberton School!');
11+
});
12+
13+
app.listen(port, host, () => {});
14+
15+
module.exports = app;

5-http/5-http.js

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
const http = require('http');
2+
const {readFile} = require('fs');
3+
4+
const port = 1245;
5+
const hostname = '127.0.0.1'
6+
7+
function countStudents(fileName){
8+
const students = {};
9+
const fields = {};
10+
let length = 0; //var, let, const
11+
12+
return new Promise((resolve, reject) => {
13+
readFile(fileName, (err, data)=>{
14+
if (err){
15+
reject(err);
16+
17+
} else {
18+
let output = '';
19+
const lines = data.toString().split('\n');
20+
for (let i = 0; i < lines.length; i += 1){
21+
if(lines[i]){
22+
length += 1;
23+
const field = lines[i].toString().split(',');
24+
if (Object.prototype.hasOwnProperty.call(students, field[3])){
25+
students[field[3]].push(field[0]);
26+
} else {
27+
students[field[3]] = [field[0]];
28+
}
29+
if (Object.prototype.hasOwnProperty.call(fields, field[3])){
30+
fields[field[3]] += 1;
31+
} else {
32+
fields[field[3]] = 1;
33+
}
34+
}
35+
}
36+
const l = length -1;
37+
output += `Number of students: ${l}\n`;
38+
for (const [key, value] of Object.entries(fields)){
39+
if (key !== 'field'){
40+
output += `Number of students in ${key}: ${value}.`;
41+
output += ` List: ${students[key].join(', ')}\n`;
42+
}
43+
}
44+
resolve(output);
45+
}
46+
});
47+
});
48+
}
49+
50+
const app = http.createServer((req, res) => {
51+
res.statusCode = 200;
52+
res.setHeader('Content-Type', 'text/plain');
53+
if (req.url === '/'){
54+
res.write('Hello Holberton School!');
55+
res.end();
56+
}
57+
if(req.url === '/students'){
58+
res.write('This is the list of our students\n');
59+
countStudents(process.argv[2]).then((output) => {
60+
const outString = output.slice(0, -1);
61+
res.end(outString);
62+
}).catch(() => {
63+
res.statusCode = 404;
64+
res.end('Cannot load the database');
65+
});
66+
}
67+
});
68+
69+
app.listen(port, hostname, () => {
70+
console.log(`Server running at http://${hostname}:${port}`);
71+
});
72+
73+
module.exports = countStudents;

5-http/database.csv

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
firstname,lastname,age,field
2+
Johann,Kerbrou,30,CS
3+
Guillaume,Salou,30,SWE
4+
Arielle,Salou,20,CS
5+
Jonathan,Benou,30,CS
6+
Emmanuel,Turlou,40,CS
7+
Guillaume,Plessous,35,CS
8+
Joseph,Crisou,34,SWE
9+
Paul,Schneider,60,SWE
10+
Tommy,Schoul,32,SWE
11+
Katie,Shirou,21,CS

6-http_express/6-http_express.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
const express = require('express');
2+
3+
const app = express();
4+
const port = 1245;
5+
6+
app.get('/', (request, response) => {
7+
response.send('Hello Holberton School!');
8+
});
9+
10+
app.listen(port, () => {
11+
console.log(`Server running at http://localhost:${port}`);
12+
})
13+
14+
module.exports = app;

7-http_express/7-http_express.js

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
const express = require('express');
2+
const {readFile} = require('fs');
3+
4+
const port = 1245;
5+
const app = express();
6+
const hostname = '127.0.0.1';
7+
8+
function countStudents(fileName){
9+
const students = {};
10+
const fields = {};
11+
let length = 0; //var, let, const
12+
13+
return new Promise((resolve, reject) => {
14+
readFile(fileName, (err, data)=>{
15+
if (err){
16+
reject(err);
17+
18+
} else {
19+
let output = '';
20+
const lines = data.toString().split('\n');
21+
for (let i = 0; i < lines.length; i += 1){
22+
if(lines[i]){
23+
length += 1;
24+
const field = lines[i].toString().split(',');
25+
if (Object.prototype.hasOwnProperty.call(students, field[3])){
26+
students[field[3]].push(field[0]);
27+
} else {
28+
students[field[3]] = [field[0]];
29+
}
30+
if (Object.prototype.hasOwnProperty.call(fields, field[3])){
31+
fields[field[3]] += 1;
32+
} else {
33+
fields[field[3]] = 1;
34+
}
35+
}
36+
}
37+
const l = length -1;
38+
output += `Number of students: ${l}\n`;
39+
for (const [key, value] of Object.entries(fields)){
40+
if (key !== 'field'){
41+
output += `Number of students in ${key}: ${value}.`;
42+
output += ` List: ${students[key].join(', ')}\n`;
43+
}
44+
}
45+
resolve(output);
46+
}
47+
});
48+
});
49+
}
50+
51+
app.get('/', (req, res) => {
52+
res.send('Hello Holberton School!');
53+
});
54+
app.get('/students', (req, res) => {
55+
countStudents(process.argv[2].toString()).then((output) =>{
56+
res.send(['This is the list of our students', output].join('\n'));
57+
}).catch(()=>{
58+
res.send('This is the list of our students\nCannot load the database');
59+
});
60+
});
61+
app.listen(port, hostname, () => {
62+
console.log(`Server running at http://${hostname}:${port}`);
63+
});
64+
65+
module.exports = countStudents;

0 commit comments

Comments
 (0)
0