forked from karim-0820/CS411B4Group2Project
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
75 lines (57 loc) · 2.01 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
// Calling all the required libraries and modules
const express = require("express");
const axios = require('axios');
const { appendFile } = require("fs");
const app = express();
const cors = require('cors');
app.use(cors({
origin: '*'
}));
const { response } = require("express");
const { get } = require("http");
// Code for generating links to random objects in the MET API
const API_URL = 'https://collectionapi.metmuseum.org/public/collection/v1/objects/';
var total = 65563; // working with a smaller library until auto-looping is up and running
function getRandomObjectURL() {
return API_URL.concat((Math.floor(Math.random() * total)).toString());
}
function getArt(res) {
// Initializations for variables used in making and responding to requests
var publicDomain;
var solution;
var imageURL;
var data;
// GET request to MET and response to frontend
do {
axios.get(getRandomObjectURL())
.then(response => {
// Storing useful data
data = response.data;
imageURL = data.primaryImageSmall;
solution = data.country;
publicDomain = data.isPublicDomain;
// If valid object, send data to frontend. Else, retry
if (publicDomain == true && solution != "") {
var package = {"image":imageURL, "solution":solution}
res.send(package);
console.log("Valid artwork requested, sent the following:");
console.log(package);
} else {
console.log("Invalid artwork, retrying.");
getArt(res);
}
})
// Error catching, mostly for calling invalid object ID's
.catch(error => {
console.log("Invalid MET Request, retrying.");
getArt(res);
});
} while (publicDomain == false || solution == "");
}
// Code for receiving get requests from frontend, making calls to API, and passing on valid info back to frontend
app.get("/", function (req, res) {
getArt(res);
});
app.listen(3000, function () {
console.log("Server is listening on localhost:3000");
});