-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
64 lines (54 loc) · 1.63 KB
/
server.js
File metadata and controls
64 lines (54 loc) · 1.63 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
const express = require('express');
const app = express();
const { Storage } = require('@google-cloud/storage');
const storage = new Storage();
/////////////////////////////////////////////////
// SET CONFIG
const BUCKET = 'shared-santa-app.appspot.com'
/////////////////////////////////////////////////
app.get('/', (req, res) => {
res.send("Hello from Shared-Santa-App! This will be the API for Santa's workshop!");
});
app.get('/v1/getAllImages', (req, res) => {
listFiles('shared-santa-app.appspot.com').then( files => {
res.send(JSON.stringify(files));
})
});
app.get('/viewWishlist', (req, res) => {
listFiles('shared-santa-app.appspot.com').then( imgUrls => {
res.send(genWishListHtml(imgUrls));
})
});
// Listen to the App Engine-specified port, or 8080 otherwise
const PORT = process.env.PORT || 8080;
app.listen(PORT, () => {
console.log(`Server listening on port ${PORT}...`);
});
function genWishListHtml(imgUrls){
let s = ''
for(const imgUrl of imgUrls) {
s += `<img src="${imgUrl}" style="width: 400px; height: auto;"><br /><br />`
}
return `
<html>
<head>
<title>My Wishlist</title>
</head>
<body>
<center>
${s}
</center>
</body>
</html>
`
}
async function listFiles(bucketName) {
// Lists files in the bucket
const [files] = await storage.bucket(bucketName).getFiles();
// Uncomment this block to print the image filenames to console
// console.log('Files:');
// for(const file of files) {
// console.log(file.name);
// }
return files.map(file => `https://storage.googleapis.com/${BUCKET}/${file.name}`);
}