-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
148 lines (129 loc) · 3.58 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
var fs=require('fs');
var data=fs.readFileSync('words.json');
//Use the JavaScript function JSON.parse() to convert text into a JavaScript object.
var words=JSON.parse(data);
let d={};
d=JSON.parse(data);
var express=require('express');
var app=express();
app.listen(3000,listening);
function listening()
{
console.log('server starting..');
console.log('The key-value pairs in words.json file');
console.log(words);
}
app.use(express.static('website'));
//To read a file and display all the contents of the file
app.get('/add/:word/:score?',addword);
function addword(request,response)
{
var data=request.params;
var word=data.word;
var score=Number(data.score);
var reply;
let info={};
//to check the length of a key
if(!score || word.length>32){
reply={
Message:"Enter valid Data"
}
}else{
//To check whether the key is present or not.
if(d.hasOwnProperty(word))
{
reply={
Message:"The Key is already exists."
}
}
else{
words[word]=score;
//Use the JavaScript function JSON.stringify() to convert it into a string.
var data=JSON.stringify(words,null,2)
//To add a single key-value pair to json file
fs.writeFile('words.json',data,finished);
function finished(err)
{
console.log('The entered data is successfully saved');
}
let info={};
let tempd={
...words
};
reply={
Status:"The key-value is saved",
Key:word,
Value:words[word],
Message:'After saving the data',
info:tempd
}
}
}
response.send(reply);
console.log('Adding data...');
console.log('After saving the key and value');
console.log(words);
}
//To read a file and displays all the contents of it
app.get('/all',sendAll);
function sendAll(request,response){
response.send(words);
}
//to find a particular Key value
app.get('/search/:word?',searchWord);
function searchWord(request,response){
var data=request.params;
var word=data.word;
var reply;
if(words[word]){
reply={
Status:"The Key is found",
Key:word,
Value:words[word]
}
}
else{
reply={
Status:"The key is not found",
Key:word
}
}
response.send(reply);
}
app.get('/delete/:word?',deleteWord);
function deleteWord(request,response){
var data=request.params;
var word=data.word;
let tempd={
...words
};
let info={};
var reply;
if (d.hasOwnProperty(word)){
//to delete a key
delete d[word];
try {
fs.writeFileSync((filePath = "./words.json"), JSON.stringify(d,null,2));
let tempd = {
...d
};
reply={
Status:'Deleted Successfully',
Message:'After Deletion',
info:tempd
}
console.log('The key is deleted');
console.log('After deletion');
console.log(tempd);
} catch (e) {
d = tempd;
}
}
else{
console.log('The key is not found');
reply={
Status:'Key is not found'
}
}
response.send(reply);
}