-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathz rest-apis-samples.js
More file actions
263 lines (191 loc) · 6.78 KB
/
z rest-apis-samples.js
File metadata and controls
263 lines (191 loc) · 6.78 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
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
252
253
254
255
256
257
258
259
260
261
262
263
// Build a function that handles a GET request to /sum with query params a and b,
// and returns a JSON response with their sum.
function findSum(req, res) {
let a = parseInt(req.query.a);
let b = parseInt(req.query.b);
res.json({ result: a + b });
}
// ---------------------------------------
// Build a function to handle a POST request to /register.
// The request body contains:
// {
// "username": "jayakrishnan",
// "password": "secret123"
// }
// Validate that both fields are present and return:
// 201 if successful.
// 400 if any field is missing.
function register(req, res) {
let { username, password } = req.body;
if (!username || !password) {
return res.status(400).json({ error: "Required fields missing!" });
}
res.status(201).json({ message: "User registered successfully." })
}
// ---------------------------------------
// Handle a GET request to /sum?a=4&b=6.
// Return a JSON like:
// { "sum": 10 }
function getSum(req, res) {
let a = parseInt(req.query.a);
let b = parseInt(req.query.b);
res.json({ sum: a + b })
}
// ---------------------------------------
// Handle GET /user/:id.
// Return this format:
// { "id": 5, "name": "Alice" }
// If ID not found, return:
// { "error": "User not found" }
function getUser(req, res) {
let id = parseInt(req.params.id);
let users = [{ id: 5, name: "Alice" }];
let user = users.find(u => u.id === id);
if (!user) {
return res.status(404).json({ error: "User not found" })
}
res.json(user);
}
// ---------------------------------------
// POST /count
// Request body:
// { "text": "hello" }
// Response:
// { "h": 1, "e": 1, "l": 2, "o": 1 }
function getCount(req, res) {
let text = req.body.text || '';
let freq = {};
for (let i of text) {
freq[i] = (freq[i] || 0) + 1;
}
res.json(freq);
}
// function getCount(req, res) {
// let text = req.body.text || '';
// let freqMap = new Map();
// for (let i = 0; i < text.length; i++) {
// if (!freqMap.has(text[i])) {
// freqMap.set(text[i], 1);
// } else {
// freqMap.set(text[i], freqMap.get(text[i]) + 1);
// }
// }
// let myObj = Object.fromEntries(freqMap);
// res.status(201).json({ result: myObj });
// }
// ---------------------------------------
// GET /palindrome?word=madam
// Return:
// { "isPalindrome": true }
function isPalindrome(req, res) {
let input = req.query.word;
let reversed = input.split('').reverse().join('');
res.json({ isPalindrome: input === reversed })
}
// function isPalindrome(req, res) {
// let input = req.query.word;
// let last = input.length - 1;
// let flag = false;
// for (let i = 0; i < input.length / 2; i++) {
// if (input[i] !== input[last]) {
// flag = true;
// break;
// }
// last--;
// }
// if (flag) {
// res.json({ isPalindrome: false });
// } else {
// res.json({ isPalindrome: true });
// }
// }
// ---------------------------------------
// Create an API endpoint that receives a POST request with name, email, and age in the request body.
// Return 400 if any field is missing.
// Return 400 if age is not a number.
// Else, return a success response with the user data.
function userData(req, res) {
const { name, email, age } = req.body;
if (!name || !email || !age) return res.status(400).json({ error: "Missing fields." });
if (typeof age !== "number") return res.status(400).json({ error: "Age is not a number" });
res.status(201).json({ name, email, age });
}
// ---------------------------------------
// Create a POST API endpoint that accepts a sentence in the body like:
// { "sentence": "Hello world, welcome to HackerRank!" }
// Return the number of words in the sentence (separated by spaces) as:
// { "wordCount": 5 }
function findWordCount(req, res) {
let sentence = req.body.sentence || '';
let words = sentence.trim().split(/\s+/).filter(Boolean);
res.json({ wordCount: words.length });
}
// function findWordCount(req, res) {
// let sentence = req.body.sentence || '';
// let count = 1;
// for (let i = 0; i < sentence.length; i++) {
// if (sentence[i] === ' ' && sentence[i + 1] !== ' ' && sentence[i + 1]) {
// count++;
// }
// }
// res.json({ wordCount: count });
// }
// -----------------------------------------------------------------------------------------------------------
// Route Logic (Method Type Awareness)
// 🧠 Question:
// You're building a route to create a new book in a library system.
// It should accept title and author in the request.
// If any field is missing, respond with 400.
// On success, respond with 201 and return the book data.
// ❓ Task:
// Which HTTP method is appropriate here? And outline the basic logic of
// your function (no need for DB, just req.body, validation, and response).
// post
// logic:
function createBook(req, res) {
const { title, author } = req.body;
if (!title || !author) {
return res.status(400).json({ error: "Missing fields." })
}
res.status(201).json({ message: 'Book created successfully.' })
}
// -----------------------------------------------------------------------------------------------------------
// ❓ Question:
// Create an endpoint GET /status that returns a simple JSON message like:
// { "status": "Server is running" }
function getStatus(req, res) {
res.json({ status: "Server is running" })
}
// -----------------------------------------------------------------------------------------------------------
// Question:
// Implement a POST /calculate route that receives two numbers (num1 and num2) in the request body,
// and returns their sum in this format:
// {
// "sum": 42
// }
// If either number is missing or not a number, return 400 with this error:
// {
// "error": "Invalid input"
// }
// function calculateSum(req, res) {
// let { num1, num2 } = req.body;
// if (typeof num1 !== 'number' || typeof num2 !== 'number') {
// return res.status(400).json({ error: 'Invalid input' });
// }
// res.status(200).json({ sum: num1 + num2 });
// }
// -----------------------------------------------------------------------------------------------------------
// Palindrome Checker
// Endpoint: GET /api/check-palindrome
// Query Param: word (string)
// Response:
// If the word is a palindrome, return:
// { "isPalindrome": true }
// Else, return:
// { "isPalindrome": false }
// 📌 Note: A palindrome reads the same forward and backward (e.g., madam, racecar).
function isPalindrome(req, res) {
let word = (req.query.word || '').toLowerCase();
let reversed = word.split('').reverse().join('');
return res.status(200).json({ isPalindrome: word === reversed });
}