Skip to content

Commit ba02f5f

Browse files
committed
Add 4-http.js file
1 parent b636e8d commit ba02f5f

File tree

4 files changed

+31
-5
lines changed

4 files changed

+31
-5
lines changed

0x05-Node_JS_basic/1-stdin.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,6 @@ process.stdin.on('readable', () => {
88
}
99
});
1010

11-
process.stdin.on('end', () => {
11+
process.stdin.on('exit', () => {
1212
process.stdout.write('This important software is now closing\n');
1313
});

0x05-Node_JS_basic/3-read_file_async.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const fs = require('fs/promises');
1+
const fs = require('fs');
22

33
/**
44
* Counts the students in a CSV data file.

0x05-Node_JS_basic/4-http.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
const http = require('http');
2+
3+
const PORT = 1245;
4+
const HOST = 'localhost';
5+
const app = http.createServer();
6+
7+
app.on('request', (_, response) => {
8+
const responseText = 'Hello Holberton School!';
9+
response.setHeader('Content-Type', 'text/plain');
10+
response.setHeader('Content-Length', responseText.length);
11+
response.statusCode = 200;
12+
response.write(Buffer.from(responseText));
13+
});
14+
15+
app.listen(PORT, HOST, () => {
16+
process.stdout.write(`Server listening at -> http://${HOST}:${PORT}\n`);
17+
});
18+
19+
module.exports = app;

0x05-Node_JS_basic/main.js

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,18 @@
1-
const countStudents = require('./2-read_file.js');
1+
const countStudents = require('./3-read_file_async');
22

3-
countStudents('nope.csv');
3+
// countStudents('nope.csv')
44
// .then(() => {
55
// console.log('Done!');
66
// })
77
// .catch((error) => {
88
// console.log(error);
99
// });
1010

11-
//countStudents('database.csv');
11+
countStudents('database.csv')
12+
.then(() => {
13+
console.log('Done!');
14+
})
15+
.catch((error) => {
16+
console.log(error);
17+
});
18+
console.log('After!');

0 commit comments

Comments
 (0)