-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
45 lines (38 loc) · 1.15 KB
/
server.js
File metadata and controls
45 lines (38 loc) · 1.15 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
const express = require('express');
require('dotenv').config();
const cors = require('cors');
const compression = require('compression');
const app = express();
const port = process.env.PORT || 8080;
const corsOptions = {
origin: [
"http://localhost:8080",
"https://vision-grid.onrender.com"
]
};
// Middleware
app.use(cors(corsOptions));
app.use(compression());
app.use(express.static('./public'));
app.get('/api/photos', async (req, res) => {
const BASE_URL = 'https://api.unsplash.com/search/photos';
const searchTerm = req.query.query;
const page = req.query.page || 1;
console.log(searchTerm, page);
const endpoint = `?query=${encodeURIComponent(
searchTerm,
)}&per_page=12&page=${page}&client_id=${process.env.CLIENT_ID}`;
try {
const response = await fetch(BASE_URL + endpoint);
const data = await response.json();
// res.flush(); // check npm compression docs
res.json(data);
} catch (err) {
console.error(err);
res.status(500).json({ error: 'Failed to fetch from Unsplash' });
}
});
app.listen(port, () => {
console.log(`Server is running http://localhost:${port}`);
console.log('Press Ctrl+C to end this process.');
});