-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
177 lines (143 loc) · 5.61 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
const http=require('http');
const fs=require('fs');
// const { isUtf8 } = require('buffer');
// const server = http.createServer((req,res) => {
// res.end('Hello, this is your Node.js server!');
// });
// const server = http.createServer((req,res) =>{
// // const bodyContent = req.body;
// // const headerContent = req.headers;
// if(req.url == '/'){
// fs.readFile('FileSystem/demo.txt','utf-8',(err,data) =>{
// if(err){
// console.error(err);
// return;
// }
// res.writeHead(200,{'Content-type': 'text/plain'});
// // res.writeHead(100,{'Content-type': 'text/plain'});
// // res.writeHead(300,{'Content-type': 'text/plain'}); // displays multiple Choices
// // res.writeHead(500,{'Content-type': 'text/plain'}); Internal server error...
// // res.writeHead(400,{'Content-type': 'text/plain'}); this gives a bad request...
// res.end('File Content:'+ data);
// // res.end(bodyContent);
// // res.end(headerContent);
// })
// }
// });
// const server=http.createServer((req,res) => {
// if(req.url === "/"){
// const message='Hello this is your node js server';
// fs.writeFile('FileSystem/demp.txt',message,'utf8',(err)=> {
// if(err){
// console.error(err);
// res.writeHead(500,{'Content-Type' : 'txt/plain'});
// res.end('Internal server error');
// return;
// }
// res.end(message);
// fs.readFile("FileSytem/demo.txt", 'utf-8',(err,data)=> {
// if(err){
// console.error(err);
// return;
// }
// // res.write(100,)
// res.writeHead(200,{'Content-Type' : 'text/plain'});
// res.end(data);
// // })
// });
// }
// });
// const server=http.createServer((req,res) => {
// if(req.url === "/"){
// try{
// const data=fs.readFileSync("FileSystem/demo.txt", 'utf8');
// res.writeHead(200,{'Content-Type' : 'text/plain'});
// res.end('File Content' + data);
// }catch (err){
// console.error(err);
// res.writeHead(500,{'Content-Type':'text/plain'});
// res.end('Internal server error');
// }
// }
// });
// const server=http.createServer((req,res) => {
// if(req.url === "/"){
// const message="This file is written using sync!";
// // fs.writeFileSyn('FileSystem/demo.txt','utf8');
// try{
// fs.writeFileSync("FileSystem/demo.txt", message,'utf8');
// res.writeHead(200,{'Content-Type' : 'text/plain'});
// res.end('File Content' + message);
// }catch (err){
// console.error(err);
// res.writeHead(500,{'Content-Type':'text/plain'});
// res.end('Internal server error');
// }
// }
// });
// // Appending file in Asynchronous Manner
// const server=http.createServer((req,res) => {
// if(req.url === "/"){
// const message='Hello this is appending using Callback';
// fs.appendFile('FileSystem/demo.txt',message +'\n','utf8',(err)=> {
// if(err){
// console.error(err);
// res.writeHead(500,{'Content-Type' : 'text/plain'});
// res.end('Internal server error');
// return;
// }else{
// res.writeHead(200,{'Content-Type' : 'text/plain'});
// res.end('FIle Content Append: ' + message);
// }
// });
// }
// });
// const server=http.createServer((req,res) => {
// if(req.url === "/"){
// try{
// const data=fs.readFileSync("FileSystem/home.html", 'utf8');
// res.writeHead(200,{'Content-Type' : 'text/html'});
// res.end('File Content' + data);
// }catch (err){
// console.error(err);
// res.writeHead(500,{'Content-Type':'text/html', 'Content-Type':' image/jpeg','Content-Type' : 'application/javascript'});
// res.end('Internal server error');
// }
// }
// });
// const server=http.createServer((req,res)=>{
// if(req==='/'){
// fs.readFile("Public/data.json",'utf8',(err,data)=>{
// if(err){
// console.error(err);
// res.end('Internal server error');
// return;
// }
// res.writeHead(200,{'Content-Type':'application/json'});
// res.end(data);
// });
// }
// });
const server=http.createServer((req,res)=>{
if(req.url==='/readfile'){
try{
const data=fs.readFileSync('FileSystem/demo.txt','utf');
res.writeHead(200,{'Content-Type':'text/plain'});
res.end('File content'+ data);
}catch(err){
console.error(err);
res.writeHead(400,{'Content-Type':'text/plain'});
}
}else{
res.writeHead(400,{'Content-Type':'text/plain'});
res.end('404 Not Found');
}
});
const port=5000;
server.listen(port, () => console.log(`Server is running on http://localhost:${port}`));
// status code : 200 -> it means it's working fine.
// 100: information
// 3xx: Redirection
// 4xx: Client Error
// 5xx: Server Error
// mdn web doc