-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
98 lines (63 loc) · 2.32 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
'use strict';
// [START app]
var app = require('express')();
var proxy = require('express-http-proxy');
//Users url segment
const users = '/users';
//Photos URL segment
const photos = '/photos/';
//Download URL segment
const download = '/download/';
//API key argument name and value
const client = '?client_id=';
const key = 'YOUR API KEY';
//Proxy serving request to URL: www.exampleHost.com/users[/...]
app.use('/users', proxy('https://API.unsplash.com', {
proxyReqPathResolver: function (req) {
//Split the path value when ? occour
//if URL is www.exampleHost.com/users/abcd/?h=x&w=y
//path will be /abcd/efg
var parts = req.url.split('?');
//if path split in more than 1 piece we are serving user profiles
if(parts.length > 1){
//get the username /abcd/
var username = parts[0];
//get the requested custom profile picture size
var custom = '&'+parts[1];
//return the url /users/abcd/?client_id=key&h=x&w=y
return users + username + client + key + custom;
}
//Asking for user's photos
else {
//in parts[0] there will be /abcd/photos
return users + parts[0] + '/' + client + key;
}
}
}));
//Proxy serving request to URL: www.exampleHost.com/photos[/...]
app.use('/photos', proxy('https://API.unsplash.com', {
proxyReqPathResolver: function (req) {
//Split the path value when ? occour
//if URL is www.exampleHost.com/photos/abcd/download
//path will be /abcd/efg
var parts = req.url.split('/');
//if path split in more than 2 piece we are serving photo download
if(parts.length > 2){
//get the photo id /abcd/
var id = parts[1];
//return the url /photos/abcd/download/?client_id=key
return photos + id + download + client + key ;
}
//Asking for single photo
else {
//in parts[1] there will be 'abcd'
return photos + parts[1] + '/' + client + key;
}
}
}));
// 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}...`);
});
// [END app]