Skip to content

Commit 7567e2f

Browse files
authored
Nodejs core module (#7)
* add: Core module * change: readline module on Core Module App.js
1 parent a9f1acc commit 7567e2f

File tree

3 files changed

+37
-9
lines changed

3 files changed

+37
-9
lines changed

4. Core Module/app.js

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
// Core module
22
// File system
33
const fs = require("node:fs");
4+
const readline = require("node:readline");
5+
const { stdin: input, stdout: output } = require("node:process");
46

57
// Write string to file synchronous
68
// try {
@@ -24,8 +26,26 @@ const fs = require("node:fs");
2426
// console.log(data);
2527

2628
// Read the content of the file Asynchronous
27-
fs.readFile("data/testAsynchronous.txt", "utf-8", (err, data) => {
28-
if (err) throw err;
2929

30-
console.log(data);
30+
// fs.readFile("data/testAsynchronous.txt", "utf-8", (err, data) => {
31+
// if (err) throw err;
32+
33+
// console.log(data);
34+
// });
35+
36+
const rl = readline.createInterface({ input, output });
37+
38+
rl.question("What your name?", (name) => {
39+
rl.question("age?", (age) => {
40+
const person = { name, age };
41+
const file = fs.readFileSync("data/persons.json", "utf-8");
42+
const persons = JSON.parse(file);
43+
44+
persons.push(person);
45+
46+
fs.writeFileSync("data/persons.json", JSON.stringify(persons));
47+
48+
console.log(`Thank you ${name}`);
49+
rl.close();
50+
});
3151
});

4. Core Module/data/persons.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
[
2+
{ "name": "Drian", "age": 18 },
3+
{ "name": "Marwan", "age": "18" },
4+
{ "name": "Hasan", "age": "10" }
5+
]

experiment/app.js

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
11
const http = require("http");
22

3-
const hostname = "localhost";
4-
const port = 9023;
3+
const hostname = "127.0.0.1";
4+
const port = 4543;
55

6-
const server = http.createServer((req, res) => {
6+
const app = http.createServer((req, res) => {
77
res.statusCode = 200;
88
res.setHeader("Content-Type", "text/plain");
9-
res.end("Hello World");
9+
res.end(
10+
"Hello Drian\
11+
sdfkjsd"
12+
);
1013
});
1114

12-
server.listen(port, hostname, () => {
13-
console.log(`Server running at http://${hostname}:${port}`);
15+
app.listen(port, hostname, () => {
16+
console.log(`Server running at http://${hostname}:${port}/`);
1417
});

0 commit comments

Comments
 (0)