-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
244 lines (193 loc) · 8.24 KB
/
index.js
File metadata and controls
244 lines (193 loc) · 8.24 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
const express = require('express');
const axios = require('axios');
const crypto = require('crypto');
const bodyParser = require('body-parser');
const app = express();
app.use(bodyParser.json());
// Marvel API credentials
const publicKey = process.env.PUBLIC_KEY;
const privateKey = process.env.PRIVATE_KEY;
app.get('/characters', (req, res) => {
const timeStamp = Date.now().toString();
const preHash = timeStamp + privateKey + publicKey;
const hash = crypto.createHash('md5').update(preHash).digest('hex');
const url = `http://gateway.marvel.com/v1/public/characters?limit=100&ts=${timeStamp}&apikey=${publicKey}&hash=${hash}`;
axios.get(url)
.then(response => {
if (!response.data.data.results.length) {
res.status(404).send('No results found.');
return;
}
res.json(response.data.data.results);
})
.catch(error => {
console.error(error);
res.status(500).send('An error occurred while fetching data from the Marvel API.');
});
});
app.get('/characters/:name', (req, res) => {
const timeStamp = Date.now().toString();
const preHash = timeStamp + privateKey + publicKey;
const hash = crypto.createHash('md5').update(preHash).digest('hex');
const name = req.params.name;
const url = `http://gateway.marvel.com/v1/public/characters?name=${name}&ts=${timeStamp}&apikey=${publicKey}&hash=${hash}`;
axios.get(url)
.then(response => {
if (!response.data.data.results.length) {
res.status(404).send('No results found for the specified name.');
return;
}
res.json(response.data.data.results);
})
.catch(error => {
console.error(error);
res.status(500).send('An error occurred while fetching data from the Marvel API.');
});
});
app.get('/character/:id', (req, res) => {
const timeStamp = Date.now().toString();
const preHash = timeStamp + privateKey + publicKey;
const hash = crypto.createHash('md5').update(preHash).digest('hex');
const characterId = req.params.id;
const url = `http://gateway.marvel.com/v1/public/characters/${characterId}?ts=${timeStamp}&apikey=${publicKey}&hash=${hash}`;
axios.get(url)
.then(response => {
if (!response.data.data.results.length) {
res.status(404).send('No results found for the specified ID.');
return;
}
res.json(response.data.data.results);
})
.catch(error => {
console.error(error);
res.status(500).send('An error occurred while fetching data from the Marvel API.');
});
});
app.get('/comics', (req, res) => {
const timeStamp = Date.now().toString();
const preHash = timeStamp + privateKey + publicKey;
const hash = crypto.createHash('md5').update(preHash).digest('hex');
const characterName = "Ant-Man (Eric O'Grady)";
if (!characterName || characterName.trim() === '') {
res.status(400).send('Character name cannot be blank.');
return;
}
// Create the URL
const url = `http://gateway.marvel.com/v1/public/characters?name=${characterName}&ts=${timeStamp}&apikey=${publicKey}&hash=${hash}`;
axios.get(url)
.then(response => {
// Check if results are empty
console.log(response.data);
if (!response.data.data.results.length) {
res.status(404).send('No results found for the specified character.');
return;
}
// Get the Spider-Man character ID
const spiderManId = response.data.data.results[0].id;
// Now use this ID to get a list of comics featuring Spider-Man
const comicsUrl = `http://gateway.marvel.com/v1/public/characters/${spiderManId}/comics?ts=${timeStamp}&apikey=${publicKey}&hash=${hash}`;
return axios.get(comicsUrl);
})
.then(response => {
if (response) {
// Return the data about the comics
res.json(response.data.data.results);
}
})
.catch(error => {
console.error(error);
res.status(500).send('An error occurred while fetching data from the Marvel API.');
});
});
app.get('/comics/:title', (req, res) => {
const timeStamp = Date.now().toString();
const preHash = timeStamp + privateKey + publicKey;
const hash = crypto.createHash('md5').update(preHash).digest('hex');
const title = req.params.title;
const url = `http://gateway.marvel.com/v1/public/comics?title=${title}&ts=${timeStamp}&apikey=${publicKey}&hash=${hash}`;
axios.get(url)
.then(response => {
if (!response.data.data.results.length) {
res.status(404).send('No results found for the specified title.');
return;
}
res.json(response.data.data.results);
})
.catch(error => {
console.error(error);
res.status(500).send('An error occurred while fetching data from the Marvel API.');
});
});
app.get('/comic/:id', (req, res) => {
const timeStamp = Date.now().toString();
const preHash = timeStamp + privateKey + publicKey;
const hash = crypto.createHash('md5').update(preHash).digest('hex');
const comicId = req.params.id;
const url = `http://gateway.marvel.com/v1/public/comics/${comicId}?ts=${timeStamp}&apikey=${publicKey}&hash=${hash}`;
axios.get(url)
.then(response => {
if (!response.data.data.results.length) {
res.status(404).send('No results found for the specified ID.');
return;
}
res.json(response.data.data.results);
})
.catch(error => {
console.error(error);
res.status(500).send('An error occurred while fetching data from the Marvel API.');
});
});
app.get('/stories', (req, res) => {
const timeStamp = Date.now().toString();
const preHash = timeStamp + privateKey + publicKey;
const hash = crypto.createHash('md5').update(preHash).digest('hex');
const url = `http://gateway.marvel.com/v1/public/stories?ts=${timeStamp}&apikey=${publicKey}&hash=${hash}`;
axios.get(url)
.then(response => {
res.json(response.data.data.results);
})
.catch(error => {
console.error(error);
res.status(500).send('An error occurred while fetching data from the Marvel API.');
});
});
app.get('/stories/:name', (req, res) => {
const timeStamp = Date.now().toString();
const preHash = timeStamp + privateKey + publicKey;
const hash = crypto.createHash('md5').update(preHash).digest('hex');
const name = req.params.name;
const url = `http://gateway.marvel.com/v1/public/stories?name=${name}&ts=${timeStamp}&apikey=${publicKey}&hash=${hash}`;
axios.get(url)
.then(response => {
if (!response.data.data.results.length) {
res.status(404).send('No results found for the specified name.');
return;
}
res.json(response.data.data.results);
})
.catch(error => {
console.error(error);
res.status(500).send('An error occurred while fetching data from the Marvel API.');
});
});
app.get('/story/:id', (req, res) => {
const timeStamp = Date.now().toString();
const preHash = timeStamp + privateKey + publicKey;
const hash = crypto.createHash('md5').update(preHash).digest('hex');
const storyId = req.params.id;
const url = `http://gateway.marvel.com/v1/public/stories/${storyId}?ts=${timeStamp}&apikey=${publicKey}&hash=${hash}`;
axios.get(url)
.then(response => {
if (!response.data.data.results.length) {
res.status(404).send('No results found for the specified ID.');
return;
}
res.json(response.data.data.results);
})
.catch(error => {
console.error(error);
res.status(500).send('An error occurred while fetching data from the Marvel API.');
});
});
const port = process.env.PORT || 3000;
app.listen(port, () => console.log(`Server is running on port ${port}`));