File tree Expand file tree Collapse file tree 3 files changed +37
-9
lines changed Expand file tree Collapse file tree 3 files changed +37
-9
lines changed Original file line number Diff line number Diff line change 11// Core module
22// File system
33const 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} ) ;
Original file line number Diff line number Diff line change 1+ [
2+ { "name" : " Drian" , "age" : 18 },
3+ { "name" : " Marwan" , "age" : " 18" },
4+ { "name" : " Hasan" , "age" : " 10" }
5+ ]
Original file line number Diff line number Diff line change 11const 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} ) ;
You can’t perform that action at this time.
0 commit comments