-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
143 lines (125 loc) · 5.07 KB
/
Copy pathserver.js
File metadata and controls
143 lines (125 loc) · 5.07 KB
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
const http = require('http');
const fs = require('fs');
const qs = require('querystring');
const url = require('url');
function handleRequest(req, res) {
var store = '';
req.on('data', (chunk) => {
store += chunk;
});
req.on('end', () => {
// ------------II. For CREATE Operation---------------------------//
if (req.method === 'POST' && req.url === '/users') {
res.statusCode = 201;
// console.log(store); // returns string
let parsedJSONData = JSON.parse(store); // converts to object
// console.log(parsedJSONData); // returns object
/*NOTE:- Open file for writing. A file is created if it doesn’t exist.
'wx'-> It is the same as ‘w’ but fails if the path exists.*/
fs.open(`./users/${parsedJSONData.username}.json`, 'wx', () => {
console.log(`${parsedJSONData.username} created!!`);
});
fs.writeFile(
`./users/${parsedJSONData.username}.json`,
store, // given store insted of parsedJSONData because writeFile() dosen't want an object passed.
(err, content) => {
err
? console.log(`Error Writing Data into users folder`)
: console.log(
`Data written successfully into ./users/${parsedJSONData.username}.json`
);
}
);
file_descriptor = fs.openSync(`./users/${parsedJSONData.username}.json`);
fs.close(file_descriptor, (err, content) => {
err
? console.log(`Error CLOSING file after it is written`)
: console.log(`./users/${parsedJSONData.username}.json File Closed`);
});
res.end(`${parsedJSONData.username} CREATED on Server!!!`);
}
// ------------II. For Read (GET) Operation---------------------------//
else if (req.method === 'GET') {
console.log(req.url);
// console.log(store);
let parsedUrl = url.parse(req.url);
console.log(parsedUrl.pathname); ///users
// console.log(qs.parse(parsedUrl.query)); // parsedUrl.query returns -> username=aseshad and qs.parse(parsedUrl) returns -> an object of that again. like { username: 'random1' }. Using this we can grab the usename to read a file with that username
let queryStringObject = qs.parse(parsedUrl.query);
console.log(queryStringObject.username); //aseshad
if (parsedUrl.pathname === '/users' && req.method === 'GET') {
fs.createReadStream(`./users/${queryStringObject.username}.json`).pipe(
res
);
}
// The above createReadStream gives the data of the particular username's (entered via url) json file data back to client, on requesting a GET request.
}
//-------------III. For UPDATE Operation -----------------------------//
else if (req.method === 'PUT') {
console.log(req.url);
let parsedUrl = url.parse(req.url);
console.log(parsedUrl.pathname); ///users
let queryStringObject = qs.parse(parsedUrl.query);
console.log(queryStringObject.username); //aseshad
fs.open(
`./users/${queryStringObject.username}.json`,
'r+', // 'r+' ensures that file already exists
(err, content) => {
//remove the content of file using `fs.ftruncate`
err
? console.log(`Error Opening the file while updating`)
: fs.ftruncate(content, () => {
err
? console.log(`Error truncating the file while updating`)
: console.log(`Truncated the existing file while updating`);
});
fs.writeFile(
`./users/${queryStringObject.username}.json`,
store,
() => {
err
? console.log(`Error writing into the file while updating`)
: console.log(`Written into file while updating with PUT!!`);
fs.close(content, (err, content) => {
err
? console.log(
`Error CLOSING file after it is updated with PUT`
)
: res.end(
`./users/${queryStringObject.username}.json File UPDATED!!`
);
});
}
);
}
);
}
//--------------IV. For Delete Opeartion -----------------------------//
else if (req.method === 'DELETE') {
console.log(req.url);
let parsedUrl = url.parse(req.url);
console.log(parsedUrl.pathname); ///users
let queryStringObject = qs.parse(parsedUrl.query);
console.log(queryStringObject.username); //aseshad
fs.unlink(
`./users/${queryStringObject.username}.json`,
(err, content) => {
err
? console.log(err)
: res.end(
`${queryStringObject.username} file DELETED from Server!!`
);
}
);
}
// Error condition - requested url is other than above routes
else {
res.statusCode = 404;
res.end(`Page Not Found!!`);
}
});
}
var server = http.createServer(handleRequest);
server.listen(4000, () => {
console.log(`Server is listening on port 4000`);
});