-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
50 lines (41 loc) · 1.27 KB
/
Copy pathserver.js
File metadata and controls
50 lines (41 loc) · 1.27 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
require('dotenv').config();
const express = require('express');
const fetch = require('node-fetch');
const cors = require('cors');
const path = require('path');
const app = express();
const PORT = process.env.PORT || 3000;
app.use(cors());
app.use(express.json());
app.use(express.static(path.join(__dirname, 'public')));
app.get('/api/search', function (req, res) {
const ingredients = req.query.ingredients;
const diet = req.query.diet;
const apiKey = process.env.SPOONACULAR_API_KEY;
if (!apiKey) {
res.status(500).json({ error: 'Missing API key' });
return;
}
const apiUrl = 'https://api.spoonacular.com/recipes/complexSearch' +
'?apiKey=' + apiKey +
'&includeIngredients=' + (ingredients || '') +
'&diet=' + (diet || '') +
'&number=10';
fetch(apiUrl)
.then(function (response) {
if (!response.ok) {
throw new Error('Failed to fetch from Spoonacular');
}
return response.json();
})
.then(function (data) {
res.json(data);
})
.catch(function (error) {
console.error('API error:', error.message);
res.status(500).json({ error: 'Something went wrong. Please try again later.' });
});
});
app.listen(PORT, function () {
console.log('Server running at http://localhost:' + PORT);
});