-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
252 lines (208 loc) · 6.61 KB
/
app.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
const express = require('express');
const bodyParser = require('body-parser');
const cors = require('cors');
const path = require('path');
const fs = require('fs');
const { get } = require('http');
const crypto = require('crypto');
const app = express();
const port = process.env.PORT || 3000;
app.use(bodyParser.json());
app.use(cors());
app.use(express.static(path.join(__dirname, 'public')));
function sha256(data) {
return crypto.createHash('sha256').update(data).digest('base64');
}
// Read word data history from the JSON file
let wordsData = [];
try {
const data = fs.readFileSync('words.json', 'utf-8');
wordsData = JSON.parse(data);
} catch (error) {
console.error('Error reading JSON file:', error.message);
}
// dictionary
let wordsDataDict = [];
let usersDict = [];
try {
const data = fs.readFileSync('output.json', 'utf-8');
wordsDataDict = JSON.parse(data);
} catch (error) {
console.error('Error reading JSON file:', error.message);
}
try {
const data = fs.readFileSync('users.json', 'utf-8');
usersDict = JSON.parse(data);
// console.log("usersDict",usersDict);
}catch (error) {
console.error('Error reading JSON file:', error.message)};
// sessions storage
let sessions = [];
try {
const data = fs.readFileSync('sessions.json', 'utf-8');
sessions = JSON.parse(data);
}catch (error) {
console.error('Error reading JSON file:', error.message)}
function addToSessions(session){
sessions.push(session);
// Update the JSON file with the new data
try {
fs.writeFileSync('sessions.json', JSON.stringify(sessions, null, 2));
} catch (error) {
console.error('Error writing to JSON file:', error.message);
}
}
// request for history of words
app.get('/api/words', (req, res) => {
cookie=req.headers.csession;
res.json(getHistory(getUserIdfromCookie(cookie)));
});
// new word adding to the words data file which is history of words
app.post('/api/words', (req, res) => {
const { word, meaning, meaningG, cookie} = req.body;
if (!word) {
return res.status(400).send( 'Enter a word.' );
}
if (!cookie){
return res.status(400).send('User not logged in. LogIn first to add words' );
}
const newWord = {
id: wordsData.length + 1,
userid: getUserIdfromCookie(cookie),
word: word,
meaningG: meaningG,
meaning: meaning
};
wordsData.push(newWord);
// Update the JSON file with the new data
try {
fs.writeFileSync('words.json', JSON.stringify(wordsData, null, 2));
} catch (error) {
console.error('Error writing to JSON file:', error.message);
}
res.json(newWord);
});
// api for dynamic search results
app.get('/api/dictionary/:word', (req, res) => {
const { word } = req.params;
const matchedWord = wordsDataDict.find((entry) => entry.word === word);
if (matchedWord) {
res.json({ meaningG: matchedWord.meaning});
} else {
res.status(404).json({ error: 'Word not found in dictionary.' });
}
});
// api for word of the day
app.get('/api/wordofday', (req, res) => {
const date = new Date();
const dayOfYear = getDayOfYear(date);
const randomIndex = dayOfYear+1500
const wordOfTheDay = wordsDataDict[randomIndex];
if (wordOfTheDay) {
res.json(wordOfTheDay);
} else {
res.status(404).json({ error: 'No word available for the day.' });
}
});
// api for login handling
app.post("/api/login",(req,res)=>{
// console.log("req",req.body);
const {username,password}=req.body;
// console.log("username",username);
// console.log("password",password);
// check for username and password in the usersDict
var matchedUser = usersDict.find((entry) => entry.username == username);
// console.log("matchedUser",matchedUser)
// matchedUser={username:"admin",password:"admin"}
if (matchedUser) {
// console.log("matchedUser",matchedUser);
if (sha256(matchedUser.password) === password) {
// console.log("matchedUser",matchedUser)
token=Math.floor(Math.random() * 1000000000000000);
res.json({ success: true, token:token });
addToSessions({userid:matchedUser.userid,token:token});
} else {
res.json({ success: false });
}
} else {
res.json({ success: false });
}
});
app.post("/api/register",(req,res)=>{
// console.log("req",req.body);
const {username,password}=req.body;
// console.log("username",username);
// console.log("password",password);
// check for username and password in the usersDict0.
var matchedUser = usersDict.find((entry) => entry.username == username);
if (matchedUser) {
// console.log("matchedUser",matchedUser);
res.json({ success: false });
} else {
const newUser = {
userid: usersDict.length + 1,
username: username,
password: sha256(password)
};
usersDict.push(newUser);
// Update the JSON file with the new data
try {
fs.writeFileSync('users.json', JSON.stringify(usersDict, null, 2));
} catch (error) {
console.error('Error writing to JSON file:', error.message);
}
token=Math.floor(Math.random() * 1000000000000000);
res.json({ success: true, token:token });
addToSessions({userid:newUser.userid,token:token});
}
});
// Function to calculate the day of the year
function getDayOfYear(date) {
const startOfYear = new Date(date.getFullYear(), 0, 0);
const diff = date - startOfYear;
const oneDay = 1000 * 60 * 60 * 24;
return Math.floor(diff / oneDay);
};
// function for finding userid from the cookie
function getUserIdfromCookie(cookie){
// console.log("cookie",cookie);
if(cookie){
matchedcookie= sessions.find((entry) => entry.token == cookie);
if (matchedcookie){
return matchedcookie.userid;
}
}
else{
return null;
}
};
// function for getting history from the userid
function getHistory(userid){
const matchedHistory = wordsData.filter((entry) => entry.userid == userid);
if (matchedHistory) {
// console.log("matchedHistory",matchedHistory);
return matchedHistory;
} else {
return [];
}
}
// handle /logout
// app.get("/logout",(req,res)=>{
// cookie=req.headers.cookie.slice(6, );
// console.log("cookie",cookie.slice(6, ));
// // console.log("cookie",cookie);
// matchedcookie= sessions.find((entry) => entry.token == cookie);
// if (matchedcookie){
// sessions.splice(sessions.indexOf(matchedcookie),1);
// // Update the JSON file with the new data
// try {
// fs.writeFileSync('sessions.json', JSON.stringify(sessions, null, 2));
// console.log("cookie deleted",cookie);
// } catch (error) {
// console.error('Error writing to JSON file:', error.message);
// }
// }
// });
app.listen(port, () => {
console.log(`Server is running on http://localhost:${port}`);
});